Bikash Santra
Indian Statistical Institute, Kolkata
import os
import shutil
# Knowing present working directory
xx = os.getcwd()
print(xx)
D:\Python ISI Chennai Teaching\1_PGDSMA
# Knowing files and folders present in the directory
yy = os.listdir(xx)
# print(yy)
for file_folder in yy:
print(file_folder)
.ipynb_checkpoints 1_Introduction.html 1_Introduction.ipynb 2_PythonBasics_I.html 2_PythonBasics_I.ipynb 3_PythonBasics_II.html 3_PythonBasics_II.ipynb 4_Numpy.html 4_Numpy.ipynb 5_Matplotlib.ipynb 6_Pandas.html 6_Pandas.ipynb 7_SystemFunctions.ipynb 8_Scipy.ipynb afile brics_test.csv brics_test.xlsx brisks_test.csv horizontal.png indexPagePGDSMA.ipynb matplotlib-logo.png numpy-logo.jpg N_brics.xlsx pythonLogo.png Pytorch_logo.png scikit-learn.png
# Change the directory
path = "D:\Python ISI Chennai Teaching"
os.chdir(path)
print(os.getcwd())
D:\Python ISI Chennai Teaching
os.chdir(xx)
print(os.getcwd())
D:\Python ISI Chennai Teaching\1_PGDSMA
# Creating new directory
dirName = xx + '/test'
# Checking existance of folder
if not os.path.isdir(dirName):
os.mkdir(dirName)
print(dirName)
D:\Python ISI Chennai Teaching\1_PGDSMA/test
# Creating chain of directories
dirName_new = dirName + '/test_1/test_2/test_3/test_4/'
os.makedirs(dirName_new)
# Checking existance of file
fileName = xx + '/afile'
if os.path.isfile(fileName):
print(fileName)
D:\Python ISI Chennai Teaching\1_PGDSMA/afile
# Copying of file/folder
sourceFile = fileName
destFile = dirName + '/test_1/test_2/test_3/afile'
shutil.copy(sourceFile, destFile)
# Copy and rename
sourceFile = dirName + '/test_1/test_2/test_3/afile'
destFile = dirName + '/test_1/a_1_file'
shutil.copy(sourceFile, destFile)
'D:\\Python ISI Chennai Teaching\\1_PGDSMA/test/test_1/a_1_file'
# Moving of file/folder
sourceFile = dirName + '/test_1/a_1_file'
destFile = dirName + '/test_1/test_2/a_1_file'
shutil.move(sourceFile, destFile)
'D:\\Python ISI Chennai Teaching\\1_PGDSMA/test/test_1/test_2/a_1_file'
# Renaming file
oldName = xx + '/' + 'N_brics.xlsx'
newName = xx + '/' + 'brics.xlsx'
os.rename(oldName,newName)
# Renaming folder
old = 'D:/Python ISI Chennai Teaching/1_PGDSMA/test/test_1/test_2'
new = 'D:/Python ISI Chennai Teaching/1_PGDSMA/test/test_1/test_X'
os.rename(old,new)
old = 'D:/Python ISI Chennai Teaching/1_PGDSMA/test/test_1/test_X'
new = 'D:/Python ISI Chennai Teaching/1_PGDSMA/test/test_1/test_2'
os.rename(old,new)
# Removing/deleting the empty directory
print(dirName_new)
os.rmdir(dirName_new)
D:\Python ISI Chennai Teaching\1_PGDSMA/test/test_1/test_2/test_3/test_4/
print(os.listdir(dirName_new[:-8]))
['afile']
os.removedirs(dirName_new[:-8])
--------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-21-d9d7b710160b> in <module> ----> 1 os.removedirs(dirName_new[:-8]) C:\ProgramData\Anaconda3\lib\os.py in removedirs(name) 239 240 """ --> 241 rmdir(name) 242 head, tail = path.split(name) 243 if not tail: OSError: [WinError 145] The directory is not empty: 'D:\\Python ISI Chennai Teaching\\1_PGDSMA/test/test_1/test_2/test_3'
# Delete folder with its contents
shutil.rmtree(dirName_new[:-8])