Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Manage Files and Folders in Python

Explore Manage Files and Folders in Python with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers manage files and folders in python with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

In this article, Quantum will work with you on managing files and directories in Python, namely creating folders, renaming folders, listing folders and working with them.

Directory in Python

In the previous article, we became familiar with the file and manipulate files in Python. Here you can simply understand that the directory is where all the files are. Sort the code as well as the file in different folders to easily manage everything.

Show Current Directory

The Getcwd () method Displays the current working directory, returning the result as a string.

You can also use the Getcwdb () method To get the result in bytes.

>>> import os

>>> os.getcwd()
'C:Program FilesPyScripter'

>>> os.getcwdb()
b'C:Program FilesPyScripter'

How to Change the Current Directory

The current working directory can be changed by the Chdir () method.

Chdir () Receives a parameter as the name of the directory you want to go from the current directory. It is possible to use either a slash (/) or a backslash () to separate elements in the path, but it is still best to use a backslash ().

>>> os.chdir('C:Python33')

>>> print(os.getcwd())
C:Python33

List of Folders and Files

You can list all files and subdirectories inside a directory using the Listdir () method.

This method takes a path and returns a list of subdirectories and files in that path.

If no path is specified, the returned result will be retrieved from the current working directory.

>>> print(os.getcwd())
C:Python33

>>> os.listdir()
['DLLs',
'Doc',
'include',
'Lib',
'libs',
'LICENSE.txt',
'NEWS.txt',
'python.exe',
'pythonw.exe',
'README.txt',
'Scripts',
'tcl',
'Tools']

>>> os.listdir('G:')
['$RECYCLE.BIN',
'Movies',
'Music',
'Photos',
'Series',
'System Volume Information']

How to Create a New Folder

To create new folders, use the Mkdir () method Of Module os.

You can choose where to store the new folder by writing the full path to the place you want to create. If the full path is not specified, the new directory will be created in the current working directory.

>>> os.mkdir('test')

>>> os.listdir()
['test']

Rename the Folder or File Name

You use the Rename () Method to Rename A folder or a file.

>>> os.listdir()
['test']

>>> os.rename('test','new_one')

>>> os.listdir()
['new_one']

How to Delete the Folder or File

To remove a file, you use the Remove () method.

Similar to deleting the entire directory, use Rmdir ()

>>> os.listdir()
['new_one', 'old.txt']

>>> os.remove('old.txt')
>>> os.listdir()
['new_one']

>>> os.rmdir('new_one')
>>> os.listdir()
[]

Note that the Rmdir () method Can only delete empty directories.

So to remove a non-empty directory, we can use the Rmtree () method inside the Shutil Module .

>>> os.listdir()
['test']

>>> os.rmdir('test')
Traceback (most recent call last):
.
OSError: [WinError 145] The directory is not empty: 'test'

>>> import shutil

>>> shutil.rmtree('test')
>>> os.listdir()
[] 

So you are familiar with the most basic operations with the directory. The os module in Python still provides a lot of other useful methods to perform operations with files and folders. Stay tuned for the next lessons of TipsMake.

Previous article: Working with File in Python

Next lesson: Error and Exception in Python

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.