in , ,

Python Directory and Files Management

Python Directory and Files Management

In this tutorial, you’ll find out about file and directory management in Python, for example, creating a directory, renaming it, listing all directories, and working with them.

Python Directory

If there are a large number of files to handle in our Python program, we can organize our code inside various registries to make things more manageable.

A directory or folder is an assortment of documents and subdirectories. Python has the os module that furnishes us with numerous valuable strategies to work with directories (and file as well).


Get Current Directory

We can get the current working directory using the getcwd() technique for the os module.

This technique restores the current working registry as a string. We can likewise utilize the getcwdb() strategy to get it as bytes object.

>>> import os

>>> os.getcwd()
'C:\\Program Files\\PyScripter'

>>> os.getcwdb()
b'C:\\Program Files\\PyScripter'

The extra backslash implies an escape sequence. The print() function will render this properly.

>>> print(os.getcwd())
C:\Program Files\PyScripter

Changing Directory

We can change the current working directory by using the chdir() method.

The new way that we need to change into must be provided as a string to this technique. We can use both the forward-slash/or the regressive slash \ to isolate the path elements.

It is safer to use an escape sequence when using the backward slash.

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

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

List Directories and Files

All files and sub-directories inside a directory can be retrieved using the listdir() method.

This method takes in a way and returns a rundown of subdirectories and documents in that way. In the event that no way is indicated, it restores the rundown of subdirectories and records 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',
'videos',
'classes',
'Series',
'System Volume Information']

Making a New Directory

We can make another directory using the mkdir() method.

This method takes in the way of the new catalog. On the off chance that the full way isn’t indicated, the new catalog is made in the current working directory.

>>> os.mkdir('test')

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

Renaming a Directory or a File

The rename() method can rename a registry or a document.

For renaming any directory or file, then rename() method takes in two fundamental contentions: the old name as the principal contention and the new name as the second argument.

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

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

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

Removing Directory or File

A file can be removed (deleted) using the remove() method.

Similarly, the rmdir() method removes an empty directory.

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

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

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

Note: The rmdir() method can only remove empty directories.

In order 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()
[]

Please feel free to give your comment if you face any difficulty here.

For more Articles click on the below link.

salman khan

Written by worldofitech

Leave a Reply

Python File I/O

Python File I/O

Python Errors and Built-in Exceptions

Python Errors and Built-in Exceptions