Skip to content

Instantly share code, notes, and snippets.

@birkin
Created December 15, 2022 21:19
Show Gist options
  • Save birkin/b9a7b6026b8f1be53d0e9aba653fbbe7 to your computer and use it in GitHub Desktop.
Save birkin/b9a7b6026b8f1be53d0e9aba653fbbe7 to your computer and use it in GitHub Desktop.
example of chatGPT output for linux filename validation

ChatGPT example

  • 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!


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment