-
the code below was produced by https://chat.openai.com/chat
-
my prompt...
show my python code that will validate that a filename can be saved on most linux servers.
(yes, i mis-typed "me")
[beginning of ChatGPT output, which consists of commentary and code]
Here is a python code that can be used to validate that a filename can be saved on most Linux servers:
import os
# List of allowed characters in a filename on Linux
allowed_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.()'
def is_valid_filename(filename):
# Check if the filename contains any characters that are not allowed
for c in filename:
if c not in allowed_chars:
return False
# Check if the filename is not empty
if not filename:
return False
# Check if the filename does not start with a period (.)
if filename[0] == '.':
return False
# Check if the filename is not too long
if len(filename) > 255:
return False
# If all the above checks pass, the filename is considered valid
return True
# Test the is_valid_filename function
assert is_valid_filename('my_file.txt') == True
assert is_valid_filename('my-file.txt') == True
assert is_valid_filename('my file.txt') == False
assert is_valid_filename('.myfile.txt') == False
assert is_valid_filename('myfile.txt' * 100) == False
This code checks if a given filename contains only allowed characters, is not empty, does not start with a period, and is not too long. If all these checks pass, the filename is considered valid.
Note that this code does not check if the filename is already taken on the server. To check for that, you will need to access the server and check if the file already exists in the desired directory.
[end of ChatGPT output]
The initial import os
line doesn't seem to be used, and our redhat-os does allow unicode filenames, but pretty cool!