File Handling In Python

File handling in Python is very simple

The most important thing is to import the os module which contains the libraries for all the functions with which you can perform file manipulations

Lets take a look at some simple code

import os
 
for fileName in os.listdir ( '/home' ):
    print fileName

This will list all the files in the directory /home

import os
 
os.mkdir("/home/user/TestDir")

This will create the TestDir in the absolute path specified as an argument to the function
import os
os.rmdir("/home/user/TestDir")

This will delete the TestDir in the absolute path specified as an argument to the function
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License