filesystems

How do I mock the filesystem in Python unit tests?

How do I mock the filesystem in Python unit tests? Question: Is there a standard way (without installing third party libraries) to do cross platform filesystem mocking in Python? If I have to go with a third party library, which library is the standard? Asked By: DudeOnRock || Source Answers: The standard mocking framework in …

Total answers: 5

How can I search sub-folders using glob.glob module?

How can I search sub-folders using glob.glob module? Question: I want to open a series of subfolders in a folder and find some text files and print some lines of the text files. I am using this: configfiles = glob.glob(‘C:/Users/sam/Desktop/file1/*.txt’) But this cannot access the subfolders as well. Does anyone know how I can use …

Total answers: 13

Python "FileExists" error when making directory

"OSError: [Errno 17] File exists" when trying to use os.makedirs Question: I have several threads running in parallel from Python on a cluster system. Each python thread outputs to a directory mydir. Each script, before outputting checks if mydir exists and if not creates it: if not os.path.isdir(mydir): os.makedirs(mydir) but this yields the error: os.makedirs(self.log_dir) …

Total answers: 5

Simplest way to get the equivalent of "find ." in python?

Simplest way to get the equivalent of "find ." in python? Question: What is the simplest way to get the full recursive list of files inside a folder with python? I know about os.walk(), but it seems overkill for just getting the unfiltered list of all files. Is it really the only option? Asked By: …

Total answers: 7

How to check type of files without extensions?

How to check type of files without extensions? Question: I have a folder full of files and they don’t have an extension. How can I check file types? I want to check the file type and change the filename accordingly. Let’s assume a function filetype(x) returns a file type like png. I want to do …

Total answers: 10

Check if file system is case-insensitive in Python

Check if file system is case-insensitive in Python Question: Is there a simple way to check in Python if a file system is case insensitive? I’m thinking in particular of file systems like HFS+ (OSX) and NTFS (Windows), where you can access the same file as foo, Foo or FOO, even though the file case …

Total answers: 10

A super strange bug of os.path.abspath

A super strange bug of os.path.abspath Question: On My Python 2.6 ( 64bit, win7, ActivePython ), when i call: os.path.abspath(‘D:/PROJECTS/SuiShouBei/www/ssb/static/voices/en/mp3/con.mp3’) It returns: ‘\\.\con’ I have no problem with other paths so far. Anyone has the same issue? Can someone please tell me why? Asked By: Vergil Lu || Source Answers: I can reproduce this in …

Total answers: 1

Use fnmatch.filter to filter files by more than one possible file extension

Use fnmatch.filter to filter files by more than one possible file extension Question: Given the following piece of python code: for root, dirs, files in os.walk(directory): for filename in fnmatch.filter(files, ‘*.png’): pass How can I filter for more than one extension? In this special case I want to get all files ending with *.png, *.gif, …

Total answers: 10

Find size and free space of the filesystem containing a given file

Find size and free space of the filesystem containing a given file Question: I’m using Python 2.6 on Linux. What is the fastest way: to determine which partition contains a given directory or file? For example, suppose that /dev/sda2 is mounted on /home, and /dev/mapper/foo is mounted on /home/foo. From the string “/home/foo/bar/baz” I would …

Total answers: 12