- Python directory
- Directory definition
- Python create directory
- Python create temporary directory
- Python rename directory
- Python remove directory
- Python move directory
- Python current working directory
- Python home directory
- Python directory parents
- Python list directory contents
- Python check if path is directory
- Python list directory recursively
- How to know/change current directory in Python shell?
- 7 Answers 7
- How to get Desktop location?
- 7 Answers 7
- How do I find the location of my Python site-packages directory?
- 21 Answers 21
- Practical Tips
- PEP 370 — Per user site-packages directory
- Abstract
- Rationale
- Specification
- Windows Notes
- Unix Notes
- Mac OS X Notes
- Implementation
Python directory
last modified September 14, 2020
Python directory tutorial shows how to work with directories in Python. We show how to create, rename, move, or list a directory in Python.
Directory definition
Directory is an organizing unit in a computer’s file system for storing and locating files. Directories are hierarchically organized into a tree of directories. Directories have parent-child relationships. A directory is sometimes also called a folder.
There are multiple functions for manipulating directories in Python. They are located in the os and pathlib modules. In the tutorial, we work with the pathlib module, which has the more modern API.
Python create directory
The Path.mkdir creates a single directory or multiple directories, including intermediate directories.
If a directory exists, a FileExistsError is raised.
The example creates a directory with Path’s mkdir function.
When we run the program twice, an exception is thrown. To get around this issue, we can check if a directory already exists.
Before we create the directory, we check if it already exists with the Path.exists function. We also catch a possible exception.
Another solution is to use the exist_ok flag. If this flag is set to True , the FileExistsError is ignored.
In this example, we ignore the FileExistsError exceptions.
Python create temporary directory
The tempfile.TemporaryDirectory function securely creates a temporary directory. On completion of the context or destruction of the temporary directory object the newly created temporary directory and all its contents are removed from the filesystem.
The example creates a temporary directory. When the program finishes, the directory is destroyed.
The program created a temporary directory in the special /tmp directory, which is used for such purposes on Linux.
Python rename directory
The Path.rename function renames a directory.
In the example, we rename the test directory to test2 .
Python remove directory
A directory is removed with the Path.rmdir function. The directory must be empty.
The example deletes the test2 directory.
Python move directory
The shutil.move function moves a directory.
The shutil.move recursively moves a file or directory (src) to another location (dst) and returns the destination.
There are two copy functions: copy and copy2 ; the default is copy2 . They differ how they handle file metadata. The copy2 tries to preserve all the file metadata, while the copy does not.
The example moves the contents of the doc directory to docs2 .
We create the docs directory and some files.
We run the program and show the contents of the new directory.
Python current working directory
The Path.cwd function returns a new path object representing the current directory.
The example changes directories and prints the current directory.
Python home directory
The Path.home function returns a new path object representing the user’s home directory.
The program prints the home directory of the user that launched it.
Python directory parents
The Path.parent retunrs the parent of the path and the Path.parents a sequence of ancestors of the path.
The example prints the various parents of the current working directory.
Python list directory contents
The Path.iterdir yields path objects of the directory contents. The children are yielded in arbitrary order, and the special entries ‘.’ and ‘..’ are not included.
The example lists the files and directories of the current working directory. The list is non-recursive.
Python check if path is directory
The Path.is_dir returns True if the path points to a directory.
In the example, we list all immediate subdirectories of the current working directory.
In our case, we have three subdirectories.
Python list directory recursively
The path.rglob yields all the files that match the given simple pattern recursively.
In the example, we recursively walk the contents of the parent directory and print all Python files.
In this tutorial, we have worked with directories in Python.
How to know/change current directory in Python shell?
I am using Python 3.2 on Windows 7. When I open the Python shell, how can I know what the current directory is and how can I change it to another directory where my modules are?
7 Answers 7
You can use the os module.
But if it’s about finding other modules: You can set an environment variable called PYTHONPATH , under Linux would be like
Then, the interpreter searches also at this place for import ed modules. I guess the name would be the same under Windows, but don’t know how to change.
edit
edit 2
. and even better: use virtualenv and virtualenv_wrapper , this will allow you to create a development environment where you can add module paths as you like ( add2virtualenv ) without polluting your installation or «normal» working environment.
In fact, os.system() can execute any command that windows command prompt can execute, not just change dir.
Changing the current directory is not the way to deal with finding modules in Python.
Rather, see the docs for The Module Search Path for how Python finds which module to import.
Here is a relevant bit from Standard Modules section:
The variable sys.path is a list of strings that determines the interpreter’s search path for modules. It is initialized to a default path taken from the environment variable PYTHONPATH, or from a built-in default if PYTHONPATH is not set. You can modify it using standard list operations:
>>> import sys
>>> sys.path.append(‘/ufs/guido/lib/python’)
In answer your original question about getting and setting the current directory:
How to get Desktop location?
I’m using Python on Windows and I want a part of my script to copy a file from a certain directory (I know its path) to the Desktop.
While txtName is the txt File’s name (with full path).
I get the error:
I want the script to work on any computer.
7 Answers 7
You can use os.environ[«HOMEPATH»] to get the path. Right now it’s literally trying to find %HOMEPATH%/Desktop without substituting the actual path.
Maybe something like:
/Desktop») works on Linux and Windows – dashesy Apr 26 ’18 at 3:36
On Unix or Linux:
and to add in your command:
/Desktop») – dashesy Apr 26 ’18 at 3:36
This works on both Windows and Linux:
\\Desktop») – Sabrina Jan 3 ’19 at 14:37
For 3.5+ you can use pathlib:
I can not comment yet, but solutions based on joining location to a user path with ‘Desktop’ have limited appliance because Desktop could and often is being remapped to a non-system drive. To get real location a windows registry should be used. or special functions via ctypes like https://stackoverflow.com/a/626927/7273599
All those answers are intrinsecally wrong : they only work for english sessions.
You should check the XDG directories instead of supposing it’s always ‘Desktop’ .
How do I find the location of my Python site-packages directory?
How do I find the location of my site-packages directory?
21 Answers 21
There are two types of site-packages directories, global and per user.
Global site-packages («dist-packages») directories are listed in sys.path when you run:
For a more concise list run getsitepackages from the site module in Python code:
Note: With virtualenvs getsitepackages is not available, sys.path from above will list the virtualenv’s site-packages directory correctly, though. In Python 3, you may use the sysconfig module instead:
The per user site-packages directory (PEP 370) is where Python installs your local packages:
If this points to a non-existing directory check the exit status of Python and see python -m site —help for explanations.
Hint: Running pip list —user or pip freeze —user gives you a list of all installed per user site-packages.
Practical Tips
.__path__ lets you identify the location(s) of a specific package: (details)
.__file__ lets you identify the location of a specific module: (difference)
to show Debian-style package information:
(or just first item with site.getsitepackages()[0] )
A solution that:
- outside of virtualenv — provides the path of global site-packages,
- insidue a virtualenv — provides the virtualenv’s site-packages
. is this one-liner:
Formatted for readability (rather than use as a one-liner), that looks like the following:
Source: an very old version of «How to Install Django» documentation (though this is useful to more than just Django installation)
It will point you to /usr/lib/pythonX.X/dist-packages
This folder only contains packages your operating system has automatically installed for programs to run.
On ubuntu, the site-packages folder that contains packages installed via setup_tools\easy_install\pip will be in /usr/local/lib/pythonX.X/dist-packages
The second folder is probably the more useful one if the use case is related to installation or reading source code.
If you do not use Ubuntu, you are probably safe copy-pasting the first code box into the terminal.
This is what worked for me:
/.local/lib/python2.7/site-packages ). – Neil Traft Jul 11 ’14 at 18:45
Let’s say you have installed the package ‘django’. import it and type in dir(django). It will show you, all the functions and attributes with that module. Type in the python interpreter —
You can do the same thing if you have installed mercurial.
This is for Snow Leopard. But I think it should work in general as well.
A modern stdlib way is using sysconfig module, available in version 2.7 and 3.2+. Unlike the current accepted answer, this method still works regardless of whether or not you have a virtual environment active.
Note: sysconfig (source) is not to be confused with the distutils.sysconfig submodule (source) mentioned in several other answers here. The latter is an entirely different module and it’s lacking the get_paths function discussed below.
Python currently uses eight paths (docs):
- stdlib: directory containing the standard Python library files that are not platform-specific.
- platstdlib: directory containing the standard Python library files that are platform-specific.
- platlib: directory for site-specific, platform-specific files.
- purelib: directory for site-specific, non-platform-specific files.
- include: directory for non-platform-specific header files.
- platinclude: directory for platform-specific header files.
- scripts: directory for script files.
- data: directory for data files.
In most cases, users finding this question would be interested in the ‘purelib’ path (in some cases, you might be interested in ‘platlib’ too). The purelib path is where ordinary Python packages will be installed by tools like pip .
At system level, you’ll see something like this:
With a venv, you’ll get something like this
The function sysconfig.get_paths() returns a dict of all of the relevant installation paths, example on Linux:
A shell script is also available to display these details, which you can invoke by executing sysconfig as a module:
PEP 370 — Per user site-packages directory
PEP: | 370 |
---|---|
Title: | Per user site-packages directory |
Author: | Christian Heimes |
Status: | Final |
Type: | Standards Track |
Created: | 11-Jan-2008 |
Python-Version: | 2.6, 3.0 |
Post-History: |
Abstract
This PEP proposes a new a per user site-packages directory to allow users the local installation of Python packages in their home directory.
Rationale
Current Python versions don’t have a unified way to install packages into the home directory of a user (except for Mac Framework builds). Users are either forced to ask the system administrator to install or update a package for them or to use one of the many workarounds like Virtual Python [1], Working Env [2] or Virtual Env [3].
It’s not the goal of the PEP to replace the tools or to implement isolated installations of Python. It only implements the most common use case of an additional site-packages directory for each user.
The feature can’t be implemented using the environment variable PYTHONPATH. The env var just inserts a new directory to the beginning of sys.path but it doesn’t parse the pth files in the directory. A full blown site-packages path is required for several applications and Python eggs.
Specification
site directory (site-packages)
A directory in sys.path. In contrast to ordinary directories the pth files in the directory are processed, too.
user site directory
A site directory inside the users’ home directory. A user site directory is specific to a Python version. The path contains the version number (major and minor only).
Unix (including Mac OS X)
/.local/lib/python2.6/site-packages Windows %APPDATA%/Python/Python26/site-packages
user data directory
Usually the parent directory of the user site directory. It’s meant for Python version specific data like config files, docs, images and translations.
Unix (including Mac)
/.local/lib/python2.6 Windows %APPDATA%/Python/Python26
user base directory
It’s located inside the user’s home directory. The user site and use config directory are inside the base directory. On some systems the directory may be shared with 3rd party apps.
Unix (including Mac)
user script directory
A directory for binaries and scripts. [10] It’s shared across Python versions and the destination directory for scripts.
Unix (including Mac)
/.local/bin Windows %APPDATA%/Python/Scripts
Windows Notes
On Windows the Application Data directory (aka APPDATA) was chosen because it is the most designated place for application data. Microsoft recommends that software doesn’t write to USERPROFILE [5] and My Documents is not suited for application data, either. [8] The code doesn’t query the Win32 API, instead it uses the environment variable %APPDATA%.
The application data directory is part of the roaming profile. In networks with domain logins the application data may be copied from and to the a central server. This can slow down log-in and log-off. Users can keep the data on the server by e.g. setting PYTHONUSERBASE to the value «%HOMEDRIVE%%HOMEPATH%Applicata Data». Users should consult their local administrator for more information. [13]
Unix Notes
/.local was chosen in favor over
/.python because the directory is already used by several other programs in analogy to /usr/local. [7] [11]
Mac OS X Notes
On Mac OS X Python uses
/.local directory as well. [12] Framework builds of Python include
/Library/Python/2.6/site-packages as an additional search path.
Implementation
The site module gets a new method adduserpackage() which adds the appropriate directory to the search path. The directory is not added if it doesn’t exist when Python is started. However the location of the user site directory and user base directory is stored in an internal variable for distutils.
The user site directory is added before the system site directories but after Python’s search paths and PYTHONPATH. This setup allows the user to install a different version of a package than the system administrator but it prevents the user from accidentally overwriting a stdlib module. Stdlib modules can still be overwritten with PYTHONPATH.
For security reasons the user site directory is not added to sys.path when the effective user id or group id is not equal to the process uid / gid [9]. It’s an additional barrier against code injection into suid apps. However Python suid scripts must always use the -E and -s option or users can sneak in their own code.
The user site directory can be suppressed with a new option -s or the environment variable PYTHONNOUSERSITE. The feature can be disabled globally by setting site.ENABLE_USER_SITE to the value False. It must be set by editing site.py. It can’t be altered in sitecustomize.py or later.
The path to the user base directory can be overwritten with the environment variable PYTHONUSERBASE. The default location is used when PYTHONUSERBASE is not set or empty.
distutils.command.install (setup.py install) gets a new argument —user to install packages in the user site directory. The required directories are created on demand.
distutils.command.build_ext (setup.py build_ext) gets a new argument —user which adds the include/ and lib/ directories in the user base directory to the search paths for header files and libraries. It also adds the lib/ directory to rpath.
The site module gets two arguments —user-base and —user-site to print the path to the user base or user site directory to the standard output. The feature is intended for scripting, e.g. ./configure —prefix $(python2.5 -m site —user-base)
distutils.sysconfig will get methods to access the private variables of site. (not yet implemented)
The Windows updater needs to be updated, too. It should create a menu item which opens the user site directory in a new explorer windows.