absolute path always begin with a drive letter 2relative path relative to the

Absolute path always begin with a drive letter

This preview shows page 9 - 19 out of 34 pages.

absolute path always begin with a drive letter. 2. relative path relative to the program's current working directory relative paths use the . and .. notation with . meaning the current directory and .. meaning the parent directory Examples: 1. absolute path E:\myfiles\python\mydata.txt 2. relative path .\mydata.txt
Image of page 9
COMMON OS MODULE METHODS Method Description getcwd() Returns a string representing the current working directory. chdir(path) Change the current working directory. mkdir(path) Create a folder. makedirs(path) Create a folder including all intermediate folders required. rename(oldname, newname) Rename a file or folder. rmdir(path) Remove a folder (folder must be empty). remove(path) Remove a file. listdir(path) Returns a list containing the files names in a folder.
Image of page 10
The mkdir(path) method creates a windows directory (folder). MKDIR(PATH) METHOD Creates a new directory named 'myprograms' in the current working directory. If the directory already exists, an error will occur. os.mkdir('myprograms') os.mkdir('e:\\test\\myprograms') Creates a new directory named 'myprograms' in the test directory on the e: drive. If the intermediate directories do not exist, an error will occur.
Image of page 11
The makedirs(path) method creates a windows directory (folder) and all of the intermediate folders. MAKEDIRS(PATH) METHOD os.mkdir('e:\\test\\myprograms') Creates a new directory named 'myprograms' in the test directory on the e: drive. If the intermediate directories do not exist, they will be created.
Image of page 12
The rename(oldname, newname) method renames a file or a directory RENAME(OLDNAME, NEWNAME) METHOD os.rename('e:\\test\\myprograms', 'e:\\test\\oldprograms') Renames the directory named 'myprograms' to 'oldprograms'. If the directory does not exist, an error will occur. os.rename('hello.py', 'helloworld.py') Renames the file named 'hello.py' to 'helloworld.py' in the current working directory. If the file does not exist, an error will occur.
Image of page 13
The rmdir(path) method deletes a directory. RMDIR(PATH) METHOD os.rmdir('e:\\test\\oldprograms') Deletes the directory named 'oldprograms'. If the directory does not exist or is not empty, an error will occur.
Image of page 14
The remove(path) method deletes a file. REMOVE(PATH) METHOD os.remove('helloworld.py') Deletes the file named 'helloworld.py' from the current working directory. If the file does not exist, an error will occur.
Image of page 15
The listdir(path) method returns a list of all file and folders in the specified directory LISTDIR(PATH) METHOD print(os.listdir('e:\\myclasses')) results in ['CPT 180', 'IST 235', 'spring_2018_schedule.docx', 'mygrades.xlsx']
Image of page 16
The Python os.path module contains many helpful methods related to file names and file paths It is part of the os module. THE OS.PATH MODULE import os
Image of page 17