11 OS&ShutilModules
11 OS&ShutilModules
• 파일탐색기를 열기
• Visit folderPractice/abcPQR/abPQ.
– MS Windows 에서는 mouse 로 더블클릭을 해 나감.
• Create a copy of list5.txt.
– Ctrl + C; Ctrl + V;
• Delete that copy.
Parent Directory vs. Subdirectory
exampleCode.py
Click here,
then you can find the absolute path.
Programming Practice
using OS Module
Move to folderPractice/Z.
You should be in folderPractice/Z.
getcwd()
• Returns current working directory as a string.
import os
print()
print(os.getcwd())
print()
C:\Users\hsko1\OneDrive\바탕 화면\folderPractice\Z
listdir()
• Returns a list containing the names of the entries in the directory given by path.
pythonTest_02.py
import os
print(os.listdir())
pythonTest_03.py
import os
parentDir = ".."
# parentDir = "../" is also fine
os.chdir(parentDir)
print(os.getcwd())
subDir = "./abcPQR"
os.chdir(subDir)
print(os.getcwd())
C:\Users\hsko1\OneDrive\바탕 화면\folderPractice
C:\Users\hsko1\OneDrive\바탕 화면\folderPractice\abcPQR
makedirs(path, exist_ok=True/False)
• Makes the directories with the specified path.
– exist_ok (optional): if the path already exists, error occurs if the value is False.
pythonTest_04.py
import os
newDir = "./MySubFolder"
# newDir = "MySubFolder" is also fine
os.makedirs(newDir)
os.makedirs(newDir) # error
os.makedirs(newDir, exist_ok=True) # ok
removedirs(path)
• Removes the directories in the specified path.
pythonTest_05.py
import os
os.removedirs("./MySubFolder")
isfile(path), isdir(path), exists(path)
• isfile(path)
– Returns True if the path is an existing file.
• isdir(path)
– Returns True if the path is an existing directory.
• exists(path)
– Returns True if there exists a file or directory.
pythonTest_06.py
import os
newDir = "./MySubFolder"
os.makedirs(newDir, exist_ok=True)
x = "./pythonTest_01.py"
y = "./MySubFolder"
print(os.path.isfile(x), os.path.isfile(y)) True False
print(os.path.isdir(x), os.path.isdir(y)) False True
print(os.path.exists(x), os.path.exists(y)) True True
rename(old, new)
• Renames the file or directory from old to new.
pythonTest_07.py
import os
oldDirName = "./MySubFolder"
newDirName = "./MyNewSubFolder"
oldFileName = "./pythonTest_01.py"
newFileName = "./pythonTest_01NEW.py"
os.rename(oldDirName, newDirName)
os.rename(oldFileName, newFileName)
Programming Practice
using Shutil Module
https://en.wikipedia.org/wiki/Shell_(computing)
Shutil
• Shell utilities
• What is Shell?
– A computer program that exposes an operating system’s services to a human user or other
programs.
– Command-Line Interface (CLI) vs. Graphical User Interface (GUI)
– You can think shell = cmd prompt in this class.
shutil_01.py
import shutil
sourceFile = "list.txt"
targetFolder = "../"
shutil.move(sourceFile, targetFolder)
Moving a Folder to a Folder
shutil_02.py
import shutil
sourceFolder = "Objs"
targetFolder = "New_Objs"
shutil.move(sourceFolder, targetFolder)
Copying a File to another File: copyfile()
shutil_03.py
import shutil
sourceFile = "Old_Maid_Hands.txt"
targetFile = "New_Old_Maid_Hands.txt"
shutil.copyfile(sourceFile, targetFile)
Copying a File to a Folder: copy()
shutil_04.py
import shutil
sourceFile = "IMG_SNU.png"
targetFolder = "Image"
shutil.copy(sourceFile, targetFolder)
# shutil.copy2(sourceFile, targetFolder)
copyfile() vs. copy() vs. copy2()
• copyfile()
– just copies the contents of the file.
• copy()
– Copyfile() + copymode
• copymode: permission to read, write, or execute
• copy2()
– Copyfile() + copystat
• copystat: metadata (permission + modification date, access date, etc.)
shutil_05.py
import shutil
sourceFolder = "abcPQR"
targetFolder = "New_abcPQR"
shutil.copytree(sourceFolder, targetFolder)
OS vs. Shutil (taken from Quora and Wikipedia)
• OS is largely thin wrappers around POSIX system calls and library functions.
– The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE
Computer Society for maintaining compatibility between operating systems.
• Shutil contains high-level Python-specific functions.
– These are built on top of Python’s os module and each call may represent dozens or hundreds of
calls to lower-level functions.
Lecture 11
OS and Shutil Modules
The End