d
Amit DhamuSoftware Engineer
 

File Operations In Python

2 minute read 00000 views
Python libraries for file operations
import os
import shutil
Current Directory
current_file = os.path.realpath(__file__)
current_directory = os.path.dirname(current_file)
cwd = os.getcwd()

print(current_file)
print(current_directory)
print(cwd)
List files in directory
for file in os.listdir(current_directory):
    print(file)
Create directory
new_directory = current_directory + '/' + 'new-directory'

if not os.path.exists(new_directory):
    os.mkdir(current_directory + '/' + 'new-directory')
Create file
new_file = new_directory + '/' + 'new-file.txt'
fo = open(new_file, 'w+')
fo.write("Python is a great language.\nYeah its great!!\n")
print("Name of the file: ", fo.name)
fo.close()
Rename a file
os.rename(new_file, new_directory + '/' + 'new-file2.txt')
new_file = new_directory + '/' + 'new-file2.txt'
Read a file
f = open(new_file, 'r')
print(f.read())
Read line by line
print(f.readline())
Delete a file
decide = input("delete the newly created file? ")
if decide == 'yes' and os.path.isfile(new_file):
    os.remove(new_file)
    print(new_file + ' deleted')
Delete a directory (only works if directory is empty)
decide = input("delete the newly created directory? ")
if decide == 'yes' and os.path.exists(new_directory):
    os.rmdir(new_directory)
    print(new_directory + ' deleted')
Delete a directory (deletes all contents inside)
decide = input("delete the newly created directory and all files inside? ")
if decide == 'yes' and os.path.exists(new_directory):
    shutil.rmtree(new_directory)
    print(new_directory + ' and all files inside deleted')