Folder separator on windows are \ but on mac or linux they are /
'c:\\something\\example.jpeg'
We escape the backslash in a string with another backslash
r'c:\something\example.jpeg'
print(r'c:\something\example.jpeg')
'\\'.join(['folder1', 'folder2', 'folder3', 'example.png'])
Above filepath will only work on windows
import os
os.path.join('folder1', 'folder2', 'folder3', 'example.png')
Above code return the path that is apporpriate for the os we are using.
os.sep
Current working directory as a string value can be obtained the following way. Directory is just a name for folder. cwd is directory where python looks when we don't specify the path for a file. i.e. only file name is specified
os.getcwd()
We can change the current working directory with change directory function chdir()
os.chdir("f:\\")
os.getcwd()
Absolute file path begins with root folder and gives complete location
Relative file path is always going to be relative with current working directory, it won't contain root folder.
single . means this directory
two . i.e. .. means parent directory
Let's say there are two folders 'one' and 'two' in my F drive, and 'one' is the cwd.
Relative path of 'one' is
.\
whereas absolulte path of 'one' is:F:\one
Relative path of the F drive is:..\
whereas absolulte path of F is:F:\
Relative path of 'two' is
..\two
whereas absolulte path of 'two' is:F:\two
If there is a file name some.txt in two, it's relative path is ..\two\some.txt
and it's absolute file path is F:\two\some.txt
Changing the current working directory back to what it was before.
os.chdir('C:\\Users\\there\\Python')
os.getcwd()
os.path.abspath('something.txt') #will return the string of the absolute path of the path that I pass it.
os.path.abspath('..\\..\\something.txt') #file is in parent folder of the parent folder
Return true if the file path is absolute, i.e. begins with a root folder.
os.path.isabs('..\\..\\something.txt')
os.path.isabs('F:\\folder\\something.txt')
Will give the relative path between two paths.
os.path.relpath('C:\\folder1\\folder2\\some.txt', 'C:\\folder1')
#will give the relative path to the first argument from the second argument which is the cwd.
Path is
F:\folder1\folder2\some.txt
os.path.dirname(r'F:\folder1\folder2\some.txt') #returns the directory part of the path.
os.path.basename('F:\\folder1\\folder2\\some.txt') #name after the last \ i.e. the file name.
Returns true if the file path actually exists and returns false if it doesn't.
os.path.exists('C:\\something.txt')
os.path.exists('C:\\Program Files')
os.path.exists('F:\\Entertainment\\movies\\Up\\Up.mkv')
os.path.isfile('F:\\Entertainment\\movies\\Up\\Up.mkv')
os.path.isfile('C:\\something_I_Made_Up')
os.path.isdir('C:\\Program Files')
os.path.isdir('C:\\something_I_Made_Up')
Above code returned false because it couldn't verify the path is a folder or a file without a extension.
os.path.isfile() and os.path.isdir() works only when the path exists otherwise they will return false.
Gives the size in bytes.
os.path.getsize('f:\\Entertainment\\Movies\\Interstellar\\Interstellar.mp4')
size = os.path.getsize('f:\\Entertainment\\Movies\\Interstellar\\Interstellar.mp4')
newsize = size/(1024*1024) #give the size in megabyte
newsize/1024 #will give the size in gigabyte
Will return a list of all the files/folders inside the path passed to it.
os.listdir('F:\\Entertainment\\Music')
listdir() is not in the path module of the os
Program to retrieve the total size of all 'files' in a folder.
totalSize = 0
for filename in os.listdir('F:\\Entertainment\\Music'):
if not os.path.isfile(os.path.join('F:\\Entertainment\\Music', filename)):
continue
totalSize = totalSize + os.path.getsize(os.path.join('F:\\Entertainment\\Music', filename))
print(totalSize)
To create new folders - can pass either absolute or relative path
os.makedirs('F:\\NewFolder1\\something\\newfolder2')
All the folders in the path above didn't existed, so python created all of them.
os.makedirs('F:\\NewFolder1\\something\\newfolder2')
#FileExistsError
try:
os.makedirs('F:\\NewFolder1\\something\\newfolder2')
except FileExistsError:
print('Directory already exists')