Login

Please fill in your details to login.





file management using python

Tricks and tips for file management using Python.
To access files in an operating system we need to use the os library. Check out the files and directories Python 3 reference for more information and functions, but I've listed the most common, most useful ones below.

File paths


Lots of these commands use a path parameter. Paths give the location of a file or directory and can be provided as the absolute location from the 'root' of the drive/home or as a relative location from the current working directory.

IMAGE OF FILE STRUCTURE

On Windows ('cause that's all I've got) ...

Absolute path

Current working directory : Anywhere!
path_to_directory = "C:\\Users\\Administrator\\Documents\\Folder"
path_to_file = "C:\\Users\\Administrator\\Documents\\Folder\\file.ext"

Relative path

Current working directory : "C:\Users\Administrator\Documents"
path_to_sub_directory = "Folder"
path_to_file = "Folder\\file.ext"
path_to_parent_directory = ".." (relative path to to C:\Users\Administrator from current working directory)
path_to_sibling_directory = "..\\Desktop"

Useful command reference


Import the os library so we can use it.

import os


Change the current working directory to path.

os.chdir(path)


Return a string containing the current working directory.

variable_identifier = os.getcwd()


Return a list containing just the names of the objects in the directory given by path which defaults to the current working directory ("."). The list is in arbitrary order, and does not include the special objects '.' and '..' even if they are present in the directory. Use this if you just want the names of the objects in the directory but remember, they may not be in the same order as they display in your file explorer.

variable_identifier = os.listdir(path='.')


Create a directory named path.

os.mkdir(path)


Remove a file given by path. This will give an error if the path is a directory/folder.

os.remove(path)


Rename the file or directory source_path to destination_path. If the destination_path already exists, this will give an error. This command is commonly used to move files/folders.

os.rename(source_path, destination_path)


Remove (delete) the directory path. If the directory does not exist or the path is a file, this will raise an error so you should check whether it exists first or use exception handling.

os.rmdir(path)


Returns a Python iterator object (an object you can loop through).

iterator = os.scandir(path)
for object in iterator:
  handle object


Retrieve the objects filename. You can get the file name without the extension using object.name.split('.')[:-1] and the extension using object.name.split('.')[-1] (notice, no colon).

variable_identifier = object.name 


The full path (including filename) of the object.

variable_identifier = object.path


Returns True if the object is a directory.

variable_identifier = object.is_dir()


Returns True if the object is a file.

variable_identifier = object.is_file()


Returns the filesize in bytes.

variable_identifier = object.stat(st_size)


Returns the time of most recent access expressed in seconds (epoch time).

variable_identifier = object.stat(st_atime)


Returns the time of most recent content modification expressed in seconds (epoch time).

variable_identifier = object.stat(st_mtime)


Returns the time of creation on Windows, expressed in seconds. For Linux, this gives you the time of the most recent metadata change.

variable_identifier = object.stat(st_ctime)


Last modified: February 26th, 2022
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning