filesystems

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

How to check whether a directory is a sub directory of another directory Question: 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, including files might be dangerous. For example, if I use the template …

Total answers: 14

finding out absolute path to a file from python

finding out absolute path to a file from python Question: 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 lives. If I invoke test.py from some directory foo, …

Total answers: 5

Elegant way to take basename of directory in Python?

Elegant way to take basename of directory in Python? Question: 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 make various files in the directory. For example, …

Total answers: 6

Get a filtered list of files in a directory

Get a filtered list of files in a directory Question: 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 and not executing ls. …

Total answers: 14

How to use glob() to find files recursively?

How to use glob() to find files recursively? Question: This is what I have: glob(os.path.join(‘src’,’*.c’)) but I want to search the subfolders of src. Something like this would work: glob(os.path.join(‘src’,’*.c’)) glob(os.path.join(‘src’,’*’,’*.c’)) glob(os.path.join(‘src’,’*’,’*’,’*.c’)) glob(os.path.join(‘src’,’*’,’*’,’*’,’*.c’)) But this is obviously limited and clunky. Asked By: Ben Gartner || Source Answers: You’ll want to use os.walk to collect filenames …

Total answers: 28

Monitoring contents of files/directories?

Monitoring contents of files/directories? Question: I’m looking for a cross-platform file monitoring python package? I know it is possible to monitor files on windows using pywin32, and there are packages working on Linux/Unix but does anyone know about a cross-platform one? Asked By: dahpgjgamgan || Source Answers: I found this link, which talks about your …

Total answers: 5

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

How to copy a directory and its contents to an existing location using Python? Question: 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 shutil.copytree() function …

Total answers: 4

How to evaluate environment variables into a string in Python?

How to evaluate environment variables into a string in Python? Question: 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 result is: “$C/test/testing” What I want to do is evaluate the environment variables …

Total answers: 3

How to list only top level directories in Python?

How to list only top level directories in Python? Question: 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 directory we have: >>> os.listdir(os.getcwd()) [‘cx_Oracle-doc’, ‘DLLs’, ‘Doc’, ‘include’, ‘Lib’, …

Total answers: 21

How to copy files?

How to copy files Question: How do I copy a file in Python? Asked By: Matt || Source Answers: shutil has many methods you can use. One of which is: import shutil shutil.copyfile(src, dst) # 2nd option shutil.copy(src, dst) # dst can be a folder; use shutil.copy2() to preserve timestamp Copy the contents of the …

Total answers: 20