5

What would be the most conservative way to check if a file-name is valid in Python on all platforms (including mobile platforms like Android, iOS)?

Ex.

this_is_valid_name.jpg -> Valid

**adad.jpg -> Invalid

a/ad -> Invalid

gat
  • 2,872
  • 2
  • 18
  • 21
  • possible duplicate of [Check whether a path is valid in Python without creating a file at the path's target](http://stackoverflow.com/questions/9532499/check-whether-a-path-is-valid-in-python-without-creating-a-file-at-the-paths-ta) – isedev Mar 13 '14 at 21:16
  • That one is about file path. If accessible or not. I am concerned with the most general file naming convention across OSes. – gat Mar 13 '14 at 21:17
  • `**adad.jpg` is a valid name e.g. in GNU/Linux: `open('**adad.jpg', 'w')` will create it. – bereal Mar 13 '14 at 21:18
  • What is your research about _valid filenames_ on the various platforms you ask for? – hc_dev Jun 08 '22 at 22:02

2 Answers2

2

The most harsh way to check if a file would be a valid filename on you target OSes is to check it against a list of properly tested filenames.

valid = myfilename in ['this_is_valid_name.jpg']

Expanding on that, you could define a set of characters that you know are allowed in filenames on every platform :

valid = set(valid_char_sequence).issuperset(myfilename)

But this is not going to be enough, as some OSes have reserved filenames.

You need to either exclude reserved names or create an expression (regexp) matching the OS allowed filename domain, and test your filename against that, for each target platform.

AFAIK, Python does not offer such helpers, because it's Easier to Ask Forgiveness than Permission. There's a lot of different possible combinations of OSes/filesystems, it's easier to react appropriately when the os raises an exception than to check for a safe filename domain for all of them.

Community
  • 1
  • 1
ddelemeny
  • 1,891
  • 13
  • 18
-1

Related topic is: "Filename Pattern Matching|.

These are the methods and functions available to you:

  1. endswith() and startswith() string methods

  2. fnmatch.fnmatch()

  3. glob.glob()

  4. pathlib.Path.glob()

import os
# Get .txt files
for f_name in os.listdir('some_directory'):
    if f_name.endswith('.txt'):
       print(f_name)

Simple Filename Pattern Matching Using fnmatch( )

import os
import fnmatch
for file_name in os.listdir('some_directory/'):
    if fnmatch.fnmatch(file_name, '*.txt'):
       print(file_name)

More Advanced Pattern Matching

for filename in os.listdir('.'):
    if fnmatch.fnmatch(filename, 'data_*_backup.txt'):
       print(filename)

Filename Pattern Matching Using glob

import glob
glob.glob('*.py')

OR Code as

import glob
for name in glob.glob('*[0-9]*.txt'):
    print(name)

OR Match as

import glob
for file in glob.iglob('**/*.py', recursive=True):
    print(file)

OR Code as

from pathlib import Path
p = Path('.')
for name in p.glob('*.p*'):
    print(name)
S.B
  • 13,077
  • 10
  • 22
  • 49