To make sure a programs output persists, scripts have to save to files.
/
on UNIX systems and C:\
on Windows)/
, Windows systems use \
. .type
suffix; it tells the OS what application handles the file. One way to construct file paths is the .join
string method:
In [159]:
'\\'.join(['folder1','folder2','folder3','file.png']) # join all elements using the escaped (literal) '\' string
Out[159]:
But this string only works on Windows; to create an OS insensitive path, using the os
module:
In [160]:
import os # contains many file path related functions
print(os.path.join('folder1','folder2','folder3','file.png')) # takes string arguments and returns OS-appropriate path
print(os.sep) # show the seperator currently being used.
If no explicit path is specified, Python will look for files in the current working directory.
You can find out what the current working directory is with os.getcwd()
, and change it is with os.chdir()
.
In [161]:
# Start at current directory
defaultpath = os.path.expanduser('~/Dropbox/learn/books/Python/AutomateTheBoringStuffWithPython/')
os.chdir(defaultpath)
print(os.getcwd())
# Change path to /files folder
os.chdir('files') # changes current working directory, if not currently in it (to /files)
print(os.getcwd()) # prints the current working directory (should be /files)
# Reset back to notebook directory
os.chdir(defaultpath)
There are two kinds of file paths, relative and absolute.
Absolute file paths are the full address, while relative paths are related to the current working directory; they start at the cwd
, not the root
folder.
There are also .
and ..
operators:
.\
refers to the cwd
, and .\path
will look at any folders below this folder...\
refers to the folder above the cwd
, and will look at any folders below the parent
folder of the cwd
.os.path.abspath
returns the absolute path of whatever relative path is passed to it.
os.path.relpath()
returns the relative path from an absolute path to a relative location.
os.path.isabs()
returns a boolean if the path is a true path.
In [162]:
print(os.path.abspath('files')) # print absolute path of the files subdirectory
print(os.path.isabs(os.path.abspath('files'))) # Is the absolute path to files an absolute path (True)
print(os.path.relpath('../..', 'files')) # print the relative file path of a folder two folders up relative to subfolder (3 folders; ../../..)
os.path.dirname()
pulls out just the directory path component above a filepath.
os.path.basename()
pulls out whats past the last slash.
In [163]:
print(os.path.dirname(os.path.abspath('files'))) # outputs absolute path above 'files'
print(os.path.basename('files/26645.pdf')) # outputs just 'files'
os.path.exists()
can check if a path exists.
os.path.isfile()
checks if the path ends at a file
os.path.isdir()
checks if the path ends at a folder.
In [164]:
# Reset back to notebook directory
os.chdir(defaultpath)
print(os.path.exists(os.path.abspath('files'))) # checks if 'files' exists (True)
print(os.path.isfile('files')) # checks if 'files' is a file (False)
print(os.path.isdir('files')) # checks if 'files' is a folder (False)
os.path.getsize()
returns the size of a file (bytes)
os.path.listdir()
returns a list of all the files at the path.
In [168]:
"""
A simple program to loop through a folder and find the size of all files in bytes, and the total size of the folder.
"""
import os
# starting size
totalSize = 0
# for the fileName in the 'files' directory
for fileName in os.listdir('files'):
# generate filePaths
filePath = os.path.join('files',fileName)
# check if filePath is a file
if os.path.isfile(filePath) == True:
# if True, increase totalSize by the size of fileName
totalSize += os.path.getsize(filePath)
# also print what the file was and the size
print('%s is %d bytes.'%(filePath, os.path.getsize(filePath)))
# otherwise keep looping
else:
continue
# print the size of the folder at the end
print('\n\nThe \'%s\' folder contains %s bytes in total.'%('files',str(totalSize)))
os.makedirs()
creates new directories at the location.
os.removedirs()
removes folders in an absolute location.
In [166]:
# clear the folders if the exist already
if os.path.exists(os.path.abspath('files/newfolder/anotherone')) == True:
os.removedirs(os.path.abspath('files/newfolder/anotherone')) # clear folders if they exist
# create new folders at an absolute path
os.makedirs(os.path.abspath('files/newfolder/anotherone')) # create new folders
# check if they exist
if os.path.exists(os.path.abspath('files/newfolder/anotherone')) == True:
print('\'files/newfolder/anotherone\' exists.')
C:\
on Windows and /
on Unix systems.\
on Windows and /
on Unix systems.os.path.join()
combines folders with the correct slash for the OS.os.getcwd()
will return the current working directory..os.chdir()
will change the current working directory..
symbol is shorthand for the current folder...
symbol is shorthand for the parent folder.os.path.abspath()
returns the absolute path form of the path given to it.os.path.isabs()
checks that a path is absolute. os.path.relpath()
returns the relative path between two paths passed to it.os.makedirs()
can make folders.os.removedirs()
can remove folders.os.path.getsize()
returns a file's size.os.listdir()
returns a list of strings of filenames.os.path.exists()
checks if a path exists.os.path.isfile()
checks if a path ends in a file.os.path.isdir()
checks if a path ends in a directory.