In Python, there are several ways to manipulate files and directories. Some of the main libraries used for this purpose are:

  • os: Library that provides a way to interact with the operating system, including file and directory manipulation. Some of the more common functions include: os.listdir() for listing the files in a directory, os.path.join() for joining directory and file paths, and os.mkdir() for creating directories.
  • pathlib: Library that provides an object-oriented way to manipulate file and directory paths. Some of the more common functions include: Path() to create a Path object, Path.glob() to find files matching a pattern, and Path.mkdir() to create a directory.
  • shutil: Library that provides high-level functions for copying, moving, and deleting files and directories. Some of the more common functions include: shutil.copy() to copy a file, shutil.move() to move a file, and shutil.rmtree() to delete a directory and all its contents.

Below is a basic example of how to list the files in a directory using the os library:

import os

directory = "/path/of/directory"
files = os.listdir(directory)

for file in files:
    print(file)

This is just a simple example of how to manipulate files and directories in Python. There are many other functions and libraries available to handle these tasks, so it's important to explore which is best suited for your specific need.

File and directory manipulation functions.

Most of Python's native file manipulation functions are equivalent to the operating system command that performs the same action, and it shouldn't be difficult to infer their usage.

os.remove('filename') #remove file
os.removedirs('directory path') #remove directory structure
os.rename('old name','new name') #rename file
os.getcwd() #return current directory
os.rmdir('directory name') #remove directory
os.mkdir('directory name') #create directory
os.chdir('directory name') #change directory (cd)
os.listdir('base directory') #list directory
os.walk('base directory') #walk through the directory tree
os.path.exists('file_name') #check if file exists
os.path.basename('file name') #returns the file name, without directory
os.path.dirname('file name') #returns the file path, without the name
os.path.getatime('file name') #returns the last access date
os.path.getctime('file name') #returns creation date
os.path.getmtime('filename') #returns the last change date
os.path.getsize('filename') #returns the file size in bytes
os.path.isdir('filename') #check if the file passed is a directory
os.path.isfile('file name') #verifies if the file passed is a file
os.path.join('dir1', 'dir2') #concatenate two elements to form a path
os.path.split('dir1', 'dir2') #returns a list of all parts of a path
shutil.copy('source','destination') #copy file
shutil.move('source','destination') #move file

 

Copyright Notice: Unless otherwise indicated, all articles are original to this site, and reproduction must cite the source

Article link:http://pybeginners.com/article/python-automation-file-and-directory-manipulation-in-python/