1092
votes
20answers
664k views

How do I check if a file exists using Python?

How do I check if a file exists, using Python, without using a try: statement?
346
votes
8answers
264k views

How do I copy a file in python?

How do I copy a file in python? I couldn't find anything under os.
293
votes
8answers
258k views

Directory listing in Python

How do I get a list of all files (and directories) in a given directory in Python?
74
votes
2answers
86k views

How to get full path of current directory in Python?

I want to get the current file's directory path. I tried: >>> os.path.abspath(__file__) 'C:\\python27\\test.py' But how can I retrieve the directory's path? For example: 'C:\\python27\\' ...
55
votes
5answers
51k views

Get a filtered list of files in a directory

I am trying to get a list of files in a directory using Python, but I do not want a list of ALL the files. What I essentially want is the ability to do something like the following but using Python ...
43
votes
10answers
33k views

How to list only top level directories in Python?

I want to be able to list only the directories inside some folder. This means I don't want filenames listed, nor do I want additional sub-folders. Let's see if an example helps. In the current ...
32
votes
5answers
26k views

Monitoring files/directories with python [duplicate]

I'm looking for a cross-platform file monitoring python package? I know it is possible to monitor files on windows usung pywin32, and there are packages working on Linux/Unix but does anyone know ...
30
votes
4answers
23k views

How to copy a directory and its contents to an existing location using Python?

I'm trying to copy a directory and all its contents to a path that already exists. The problem is, between the os module and the shutil module, there doesn't seem to be a way to do this. the ...
26
votes
1answer
748 views

A super strange bug of os.path.abspath

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 ...
18
votes
6answers
17k views

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

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 ...
14
votes
3answers
7k views

Check whether a path is valid in Python without creating a file at the path's target

I have a I have a path (including directory and file name). I need to test if the file-name is a valid, e.g. if the file-system will allow me to create a file with such a name. The file-name has some ...
12
votes
3answers
7k views

Convert a directory structure in the filesystem to JSON with Node.js

I have a file structure like this: root |_ fruits |___ apple |______images |________ apple001.jpg |________ apple002.jpg |_ animals |___ cat |______images |________ cat001.jpg |________ cat002.jpg ...
12
votes
3answers
797 views

Check if an open file has been deleted after open in python

Is it possible to check if a file has been deleted or recreated in python? For example, if you did a open("file") in the script, and then while that file is still open, you do rm file; touch file;, ...
10
votes
1answer
4k views

Evaluate environment variables into a string

I have a string representing a path. Because this application is used on Windows, OSX and Linux, we've defined environment variables to properly map volumes from the different file systems. The ...
10
votes
2answers
609 views

Name of files opened by a process in window?

How to print name of file open by some process (PID) in window? Or All Processes (PID) currently open a file. Process Explorer is a utility works for same. But how does it work not mentioned? ...
10
votes
4answers
5k views

Cross platform hidden file detection

What is the best way to do cross-platform handling of hidden files? (preferably in Python, but other solutions still appreciated) Simply checking for a leading '.' works for *nix/Mac, and file ...
10
votes
2answers
514 views

Check if file system is case-insensitive in Python

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, ...
10
votes
6answers
5k views

How to find the mountpoint a file resides on?

For example, I've got a file with the following path: /media/my_mountpoint/path/to/file.txt I've got the whole path and want to get: /media/my_mountpoint How can I do this? Preferably in Python ...
10
votes
5answers
2k views

Monitor events in a filesystem as they happen

I want to to write a script in either Ruby or Python that will look at the contents of a directory and monitor for 1. Any new files 2. Any saves to existing files. I'm not bothered about what is ...
10
votes
1answer
517 views

How do I mock the filesystem in python unit tests

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?
9
votes
3answers
3k views

Weird problem with input encoding in IPython

I'm running python 2.6 with latest IPython on Windows XP SP3, and I have two questions. First one of my problems is, when under IPython, I cannot input Unicode strings directly, and, as a result, ...
9
votes
4answers
3k views

How to check whether a directory is a sub directory of another directory

I like to write a template system in Python, which allows to include files. e.g. This is a template You can safely include files with safe_include`othertemplate.rst` As you know, ...
7
votes
5answers
5k views

Elegant way to take basename of directory in Python?

I have several scripts that take as input a directory name, and my program creates files in those directories. Sometimes I want to take the basename of a directory given to the program and use it to ...
7
votes
4answers
7k views

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

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? ...
7
votes
2answers
2k views

List files in a folder as a stream to begin process immediately

I get a folder with 1 million files in it. I would like to begin process immediately, when listing files in this folder, in Python or other script langage. The usual functions (os.listdir in ...
7
votes
5answers
2k views

finding out absolute path to a file from python

if I have a file test.py that resides in some directory, how can I find out from test.py what directory it is in? os.path.curdir will give the current directory but not the directory where the file ...
7
votes
2answers
7k views

Absolute path of a file object

This has been discussed on StackOverflow before - I am trying to find a good way to find the absolute path of a file object, but I need it to be robust to os.chdir(), so cannot use f = file('test') ...
7
votes
1answer
1k views

Why is the argument of os.umask() inverted? (umask 0o000 makes chmod 0o777)

In most places, permissions are defined as an octal number in the format of 0777. But UNIX's umask command (thus os.umask()) needs 0o000 to produce the permission bits of 0o777 and 0o022 equals to ...
6
votes
7answers
3k views

How do I determine an open file's size in Python?

There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be ...
6
votes
4answers
3k views

detect new or modified files with python

I'm trying to detect when a new file is created a directory or when an existing file is modified in a directory. I tried searching for a script that would do this (preferably in python or bash) but ...
6
votes
2answers
525 views

How to determine if a directory is on same partition

Say I have an input file, and a target directory. How do I determine if the input file is on the same hard-drive (or partition) as the target directory? What I want to do is the copy a file if it's ...
6
votes
4answers
2k views

easiest way to program a virtual file system in windows with Python

I want to program a virtual file system in Windows with Python. That is, a program in Python whose interface is actually an "explorer windows". You can create & manipulate file-like objects but ...
6
votes
1answer
4k views

List directory file contents in a Django template

I'm just learning Python & Django. (Thanks to everyone who contributes here -- it's been an invaluable resource!) One seemingly basic thing that I'm having trouble with is rendering a simple ...
6
votes
3answers
4k views

How to check for hidden files & folders on NTFS partition using python on linux?

I am using NTFS partition on linux machine. I want to identify hidden files and folders on my NTFS partition on linux using python. How can I achieve this using python. Any code snippet / links would ...
6
votes
2answers
1k views

Determine cluster size of file system in Python

I would like to calculate the "size on disk" of a file in Python. Therefore I would like to determine the cluster size of the file system where the file is stored. How do I determine the cluster ...
5
votes
4answers
722 views

Where does python open files to on a mac?

I made a simple python script which creates a text file. The contents of the script are f = open("New", "w") f.close Now I moved the script to the desktop (I'm on a mac) and ran it, and nothing ...
5
votes
2answers
3k views

Python “FileExists” error when making directory

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 ...
5
votes
1answer
2k views

How to create a very large file cheaply using Python in Windows 7? [duplicate]

Possible Duplicate: Quickly create large file on a windows system? For testing purposes, I would like to be able to create and delete very large files (several GB's). I have no need to ...
5
votes
1answer
5k views

using wild card when listing directories in python

how can I use wild cars like '*' when getting a list of files inside a directory in Python? for example, I want something like: os.listdir('foo/*bar*/*.txt') which would return a list of all the ...
5
votes
3answers
2k views

Python - Make Script to Manipulate Windows File Paths but running on Linux

I have this script which processes lines containing windows file paths. However the script is running on Linux. Is there a way to change the os library to do Windows file path handling while running ...
5
votes
4answers
1k views

How to determine number of files on a drive with Python?

I have been trying to figure out how to retrieve (quickly) the number of files on a given HFS+ drive with python. I have been playing with os.statvfs and such, but can't quite get anything (that ...
5
votes
2answers
622 views

How to loop through files and rename them in Python

I have a directory of music that has album folders as well as individual songs on each level. How can I traverse all of these files that also are encoded in different formats(mp3, wav etc)? In ...
5
votes
2answers
154 views

Is there a standard way, across operating systems, of adding “tags” to files

I'm writing a script to make backups of various different files. What I'd like to do is store meta information about the backup. Currently I'm using the file name, so for example: ...
5
votes
2answers
261 views

How to change file system encoding via python?

>>> import sys >>> sys.getfilesystemencoding() 'UTF-8' How do I change that? I know how to change the default system encoding. >>> reload(sys) <module 'sys' ...
4
votes
5answers
805 views

disallow access to filesystem inside exec and eval in Python

I want to disallow access to file system from clients code, so I think I could overwrite open function env = { 'open': lambda *a: StringIO("you can't use open") } exec(open('user_code.py'), env) ...
4
votes
4answers
6k views

Python mkdir giving me wrong permissions

I'm trying to create a folder and create a file within it. Whenever i create that folder (via Python), it creates a folder that gives me no permissions at all and read-only mode. When i try to ...
4
votes
1answer
329 views

Is a file on the same filesystem as another file in python?

Is there a simple way of finding out if a file is on the same filesystem as another file? The following command: import shutil shutil.move('filepatha', 'filepathb') will try and rename the file ...
4
votes
2answers
3k views

Unicode encoding for filesystem in Mac OS X not correct in Python?

Having a bit of struggle with Unicode file names in OS X and Python. I am trying to use filenames as input for a regular expression later in the code, but the encoding used in the filenames seem to be ...
4
votes
1answer
1k views

How to implement a Python virtual filesystem using shelve

I have set up a Python script that simulates an OS. It has a command prompt and a virtual file system. I am using the shelve module to simulate the file system, being multi-dimensional in order to ...
4
votes
1answer
468 views

Python's os.chdir() and os.getcwd() mismatch when using tempfile.mkdtemp() on Mac OSX Lion

I'm not sure if this is a bug or a feature, but when I change the directory using os.chdir() to the one generated with tempfile.mkdtemp() then os.getcwd() reports a directory with /private prefix. ...