1

I am currently working on a python project on my macintosh, and from time to time I get unexpected errors, because .DS or other files, which are not visible to the "not-root-user" are found in folders. I am using the following command

filenames = getfiles.getfiles(file_directory)

to retreive information about the amount and name of the files in a folder. So I was wondering, if there is a possibility to prevent the getfiles command to see these types of files, by for example limiting its right or the extensions which it can see (all files are of .txt format)

Many thanks in advance!

3 Answers3

0

There isn't an option to filter within getfiles, but you could filter the list after.

Most-likely you will want to skip all "dot files" ("system files", those with a leading .), which you can accomplish with code like the following.

filenames = [f for f in ['./.a', './b'] if not os.path.basename(f).startswith('.')]
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
0

In your case, I would recommend you switch to the Python standard library glob.

In your case, if all files are of .txt format, and they are located in the directory of /sample/directory/, you can use the following script to get the list of files you wanted.

from glob import glob
filenames = glob.glob("/sample/directory/*.txt")

You can easily use regular expressions to match files and filter out files you do not need. More details can be found from Here.

Keep in mind that with regular expression, you could do much more complicated pattern matching than the above example to handle your future needs.

Another good example of using glob to glob multiple extensions can be found from Here.

If you only want to get the basenames of those files, you can always use standard library os to extract basenames from the full paths.

import os
file_basenames = [os.path.basename(full_path) for full_path in filenames]
Xinyao Wang
  • 2,029
  • 12
  • 24
0

Welcome to Stackoverflow.

You might find the glob module useful. The glob.glob function takes a path including wildcards and returns a list of the filenames that match.

This would allow you to either select the files you want, like

filenames = glob.glob(os.path.join(file_directory, "*.txt")

Alternatively, select the files you don't want, and ignore them:

exclude_files = glob.glob(os.path.join(file_directory, ".*"))
for filename in getfiles.getfiles(file_directory):
    if filename in exclude_files:
        continue
    # process the file
holdenweb
  • 33,305
  • 7
  • 57
  • 77