This program covers directory commands: Remove, create, change to, list directory
os.getcwd(): get current working directory
os.listdir(): list directories
os.mkdir(): make (create) a directory
os.chdir(): Change directory
Write_File_List.py
#!/usr/bin/env python
import os
path = “/home/michael/PycharmProjects/CBenson”
filelist = []
for root, dirs, files in os.walk(path):
for file in files:
#append the file name to the list
filelist.append(os.path.join(root,file))
#Writing a file that has a list of files for the directories
#filename that will be appended to
filename=”directory_list.txt”
myfile=open(filename, ‘a’)
#print all the file names
for name in filelist:
myfile.write(name)
myfile.write(‘\n’)
Chapter3.py
#!/usr/bin/env python
import os
print(‘Commands to work with directory and file commands’)
print(‘ ‘)
cwd=os.getcwd()
print (‘current working directory’,cwd )
# this removes the directory Chapter3_test so I can create it.
os.rmdir(‘Chapter3_test’)
listdir=os.listdir()
print (‘list directories’, listdir)
print (‘ ‘)
os.mkdir(‘Chapter3_test’)
listdir=os.listdir()
print (‘list directories’, listdir)
print (‘ ‘)
cwd=os.getcwd()
print (‘current working directory’,cwd )
print (‘change to Chapter3_test’)
print (‘ ‘)
chdir = os.chdir(“Chapter3_test”)
cwd=os.getcwd()
print (‘current working directory’,cwd )
Output from the program
/home/michael/PycharmProjects/untitled/venv/bin/python /home/michael/PycharmProjects/CBenson/Chapter3.py
Commands to work with directory and file commands
current working directory /home/michael/PycharmProjects/CBenson
list directories [‘.idea’, ‘write_file_list.py’, ‘directory_list.txt’, ‘venv’, ‘Chapter3.py’]
list directories [‘.idea’, ‘write_file_list.py’, ‘directory_list.txt’, ‘Chapter3_test’, ‘venv’, ‘Chapter3.py’]
current working directory /home/michael/PycharmProjects/CBenson
change to Chapter3_test
current working directory /home/michael/PycharmProjects/CBenson/Chapter3_test
Process finished with exit code 0
DirectoryList.py
/home/michael/PycharmProjects/CBenson/Chapter3.py
/home/michael/PycharmProjects/CBenson/.idea/.gitignore
/home/michael/PycharmProjects/CBenson/.idea/misc.xml
/home/michael/PycharmProjects/CBenson/.idea/CBenson.iml
/home/michael/PycharmProjects/CBenson/.idea/modules.xml
/home/michael/PycharmProjects/CBenson/.idea/workspace.xml
/home/michael/PycharmProjects/CBenson/.idea/inspectionProfiles/profiles_settings.xml
/home/michael/PycharmProjects/CBenson/venv/pyvenv.cfg
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/easy_install.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/__init__.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/archive_util.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/cli-64.exe
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/_deprecation_warning.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/version.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/errors.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/sandbox.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/gui.exe
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/glob.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/windows_support.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/launch.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/namespaces.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/py34compat.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/py31compat.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/config.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/wheel.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/package_index.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/gui-64.exe
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/extension.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/py27compat.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/cli.exe
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/dep_util.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/cli-32.exe
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/dist.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/py33compat.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/site-patch.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/_imp.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/script.tmpl
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/gui-32.exe
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/monkey.py
/home/michael/PycharmProjects/CBenson/venv/lib/python3.8/site-packages/setuptools/msvc.py