Python Housekeeping and Maintenance Flashcards
library module offers features
that help us deal with our local file
system
os
import os
os function that gets the current directory
os.getcwd()
os function that walks the elements of a directory tree.
os.walk()
os function that lists the directory of the specified path
os.listdir(path)
os function that returns the absolute path of the specified file name
os.path.abspath(name)
os function that returns true if the specified file or directory name exists
os.path.exists(name)
os function that returns true if the specified name is a directory.
os.path.isdir(name)
os function that returns true if the specified name is a file
os.path.isfile(name)
method to determine if file has a specific ending
.filename.endswith(‘txt’)
method to perform concatenation that accounts for path delimiter difference across operating platforms ( ‘/’ or ‘\’)
.join()
from os.path import join
thefile= os.path.join(dirname, filename)
os method to return the file size
.getsize()
os.path.getsize(thefile)
os method that deletes a file
.remove()
os.remove(thefile)
method to create a pipe that connects the output from one execution to the input of another execution
.popen()
cmd = ‘ls -l’
file_pointer = os.popen(cmd)
all_lines = file_pointer.read()
print(all_lines)
status = file_pointer.close()
print(status)