The os.path module contains many helpful functions related to filenames and file paths. Since os.path is a module inside the os module, you can import it by simply running import os. Lets us look at some some methods in the os.path module.
1. os.path.join()
If we pass it the string values of individual file and folder names in our path, os.path.join() will return a string with a file path using the correct path separators. Enter the following into the interactive shell:
import os
os.path.join('Python_Code','examples','MyCode')
The output you'll see is:
'Python_Code\\examples\\MyCode'
Since I'm running these interactive shell examples on Windows, so os.path.join('Python_Code','examples','MyCode') returned 'Python_Code\\examples\\MyCode'. The backslashes are doubled because each backslash needs to be escaped by another backslash character.
The os.path.join() function is helpful if you need to create strings for filenames. For example, the following example joins names from a list of filenames to the end of a folder’s name:
import os
file_list = ['euler.txt','newcities.txt','m2.jpg']
for file_name in file_list:
print(os.path.join('F:\\Text_files',file_name))
The output of the program will be:
F:\Text_files\euler.txt
F:\Text_files\newcities.txt
F:\Text_files\m2.jpg
2. Functions for current working directory
We can get the current working directory as a string value with the os.getcwd() function and change
it with os.chdir().
Enter the following into the interactive shell:
>>> import os
>>> os.getcwd()
I output I got is 'C:\\Users\\Python'. If I want to change my current working directory to F:\Text_files I'll use the os.chdir() as follows:
os.chdir('F:\\Text_files')
Now if I run >>> os.getcwd() my output will be:
'F:\\Text_files'
3. Handling Absolute and Relative Paths
In the os.path module there are functions for returning the absolute path of a relative path and for checking whether a given path is an absolute path. An absolute path, which always begins with the root folder. A relative path, which is relative to the program’s current working directory. There are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that can be used in a path. A single period (“dot”) for a folder name is shorthand for “this directory.” Two periods (“dot-dot”) means “the parent folder.”
a. Calling os.path.abspath(path) will return a string of the absolute path of the argument. This is an easy way to convert a relative path into an absolute one. So if I type:
>>> os.path.abspath('.')
The output is:
'C:\\Users\\Python'
b. Calling os.path.isabs(path) will return True if the argument is an absolute path and False if it is a relative path. So if I type:
>>> os.path.isabs('.')
I get the output False. However this input gives true:
>>> os.path.isabs(os.path.abspath('.'))
True
c. Calling os.path.relpath(path, start) will return a string of a relative path from the start path to path. If start is not provided, the current working directory is used as the start path. So if I type:
>>> os.path.relpath('\Python')
'..\\..\\Python'
>>> os.path.relpath('C:\\Users','C:\\')
'Users'
Two periods (“dot-dot”) means “the parent folder.”
d. The os.path.dirname(path) will return a string of everything that comes before the last slash in the path argument. Calling os.path.basename(path) will return a string of everything that comes after the last slash in the path argument. Enter the following into the interactive shell:
>>> path = 'C:\\Users\\Python\\calc.exe'
>>> os.path.basename(path)
'calc.exe'
>>> os.path.dirname(path)
'C:\\Users\\Python'
If we need a path’s dir name and base name together, you can just call os.path.split() to get a tuple value with these two strings, like so:
>>> myFilePath = 'C:\\Users\\Python\\calc.exe'
>>> os.path.split(myFilePath)
('C:\\Users\\Python', 'calc.exe')
The os.path.split() does not take a file path and return a list of strings of each folder. For that, use the split() string method and split on the string in os.sep variable which is set to the correct folder-separating slash for the computer running the program. For example, enter the following into the interactive shell:
>>>myFilePath.split(os.path.sep)
['C:', 'Users', 'Python', 'calc.exe']
4.Finding File Sizes and Folder Contents
The os.path module provides functions for finding the size of a file in bytes and the files and folders
inside a given folder.
a. Calling os.path.getsize(path) will return the size in bytes of the file in the path argument. For example:
>>> os.path.getsize('C:\\Users\\Python\\calc.exe')
776192
b. Calling os.listdir(path) will return a list of filename strings for each file in the path argument. But this function is in the os module, not os.path. For example:
>>> os.listdir('C:\\Users\\Python\\calc.exe')
['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',
--snip--
'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']
If I want to find the total size of all the files in a directory, I can use os.path.getsize() and os.listdir() together. For example:
dirSize = 0
for file_name in os.listdir('C:\\Users\\Python'):
dirSize = dirSize + os.path.getsize(os.path.join('C:\\Users\\Python\\, file_name))
print(dirSize )
The output of this program is:
1117846456
As we loop over each file_name in the C:\\Users\\Python folder, the dirSize variable is incremented by the size of each file. Notice how when I call os.path.getsize(), I use os.path.join() to join the folder name with the current filename. The integer that os.path.getsize() returns is added to the value of dirSize. After looping through all the files, I print dirSize to see the total size of the C:\\Users\\Python folder.
5. Checking Path Validity
The os.path module provides functions to check whether a given path exists and whether it is a file or folder.
• Calling os.path.exists(path) will return True if the file or folder referred to in the argument exists and will return False if it does not exist.
• Calling os.path.isfile(path) will return True if the path argument exists and is a file and will return False otherwise.
• Calling os.path.isdir(path) will return True if the path argument exists and is a folder and will return False otherwise.
Here are some of the implementations:
>>> os.path.exists('C:\\Users')
True
>>> os.path.exists('C:\\mycode')
False
>>> os.path.isdir('C:\\Users\\Python')
True
>>> os.path.isfile('C:\\Users\\Python')
False
>>> os.path.isdir('C:\\Users\\Python\\calc.exe')
False
>>> os.path.isfile('C:\\Users\\Python\\calc.exe')
True
We can determine whether there is a DVD or flash drive currently attached to the computer by checking for it with the os.path.exists() function. For instance, if I wanted to check for a flash drive with the volume named D:\ on my Windows computer, I could do that with the following:
>>> os.path.exists('D:\\')
False
These were some of the commonly used functions from the os.path module but if your interested to explore os.path module in details, the full documentation for the os.path module is on the Python website at https://docs.python.org/3/library/os.path.html
Till we meet next, keep practicing and learning Python as Python is easy to learn!
1. os.path.join()
If we pass it the string values of individual file and folder names in our path, os.path.join() will return a string with a file path using the correct path separators. Enter the following into the interactive shell:
import os
os.path.join('Python_Code','examples','MyCode')
The output you'll see is:
'Python_Code\\examples\\MyCode'
Since I'm running these interactive shell examples on Windows, so os.path.join('Python_Code','examples','MyCode') returned 'Python_Code\\examples\\MyCode'. The backslashes are doubled because each backslash needs to be escaped by another backslash character.
The os.path.join() function is helpful if you need to create strings for filenames. For example, the following example joins names from a list of filenames to the end of a folder’s name:
import os
file_list = ['euler.txt','newcities.txt','m2.jpg']
for file_name in file_list:
print(os.path.join('F:\\Text_files',file_name))
The output of the program will be:
F:\Text_files\euler.txt
F:\Text_files\newcities.txt
F:\Text_files\m2.jpg
2. Functions for current working directory
We can get the current working directory as a string value with the os.getcwd() function and change
it with os.chdir().
Enter the following into the interactive shell:
>>> import os
>>> os.getcwd()
I output I got is 'C:\\Users\\Python'. If I want to change my current working directory to F:\Text_files I'll use the os.chdir() as follows:
os.chdir('F:\\Text_files')
Now if I run >>> os.getcwd() my output will be:
'F:\\Text_files'
3. Handling Absolute and Relative Paths
In the os.path module there are functions for returning the absolute path of a relative path and for checking whether a given path is an absolute path. An absolute path, which always begins with the root folder. A relative path, which is relative to the program’s current working directory. There are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that can be used in a path. A single period (“dot”) for a folder name is shorthand for “this directory.” Two periods (“dot-dot”) means “the parent folder.”
a. Calling os.path.abspath(path) will return a string of the absolute path of the argument. This is an easy way to convert a relative path into an absolute one. So if I type:
>>> os.path.abspath('.')
The output is:
'C:\\Users\\Python'
b. Calling os.path.isabs(path) will return True if the argument is an absolute path and False if it is a relative path. So if I type:
>>> os.path.isabs('.')
I get the output False. However this input gives true:
>>> os.path.isabs(os.path.abspath('.'))
True
c. Calling os.path.relpath(path, start) will return a string of a relative path from the start path to path. If start is not provided, the current working directory is used as the start path. So if I type:
>>> os.path.relpath('\Python')
'..\\..\\Python'
>>> os.path.relpath('C:\\Users','C:\\')
'Users'
Two periods (“dot-dot”) means “the parent folder.”
d. The os.path.dirname(path) will return a string of everything that comes before the last slash in the path argument. Calling os.path.basename(path) will return a string of everything that comes after the last slash in the path argument. Enter the following into the interactive shell:
>>> path = 'C:\\Users\\Python\\calc.exe'
>>> os.path.basename(path)
'calc.exe'
>>> os.path.dirname(path)
'C:\\Users\\Python'
If we need a path’s dir name and base name together, you can just call os.path.split() to get a tuple value with these two strings, like so:
>>> myFilePath = 'C:\\Users\\Python\\calc.exe'
>>> os.path.split(myFilePath)
('C:\\Users\\Python', 'calc.exe')
The os.path.split() does not take a file path and return a list of strings of each folder. For that, use the split() string method and split on the string in os.sep variable which is set to the correct folder-separating slash for the computer running the program. For example, enter the following into the interactive shell:
>>>myFilePath.split(os.path.sep)
['C:', 'Users', 'Python', 'calc.exe']
4.Finding File Sizes and Folder Contents
The os.path module provides functions for finding the size of a file in bytes and the files and folders
inside a given folder.
a. Calling os.path.getsize(path) will return the size in bytes of the file in the path argument. For example:
>>> os.path.getsize('C:\\Users\\Python\\calc.exe')
776192
b. Calling os.listdir(path) will return a list of filename strings for each file in the path argument. But this function is in the os module, not os.path. For example:
>>> os.listdir('C:\\Users\\Python\\calc.exe')
['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',
--snip--
'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']
If I want to find the total size of all the files in a directory, I can use os.path.getsize() and os.listdir() together. For example:
dirSize = 0
for file_name in os.listdir('C:\\Users\\Python'):
dirSize = dirSize + os.path.getsize(os.path.join('C:\\Users\\Python\\, file_name))
print(dirSize )
The output of this program is:
1117846456
As we loop over each file_name in the C:\\Users\\Python folder, the dirSize variable is incremented by the size of each file. Notice how when I call os.path.getsize(), I use os.path.join() to join the folder name with the current filename. The integer that os.path.getsize() returns is added to the value of dirSize. After looping through all the files, I print dirSize to see the total size of the C:\\Users\\Python folder.
5. Checking Path Validity
The os.path module provides functions to check whether a given path exists and whether it is a file or folder.
• Calling os.path.exists(path) will return True if the file or folder referred to in the argument exists and will return False if it does not exist.
• Calling os.path.isfile(path) will return True if the path argument exists and is a file and will return False otherwise.
• Calling os.path.isdir(path) will return True if the path argument exists and is a folder and will return False otherwise.
Here are some of the implementations:
>>> os.path.exists('C:\\Users')
True
>>> os.path.exists('C:\\mycode')
False
>>> os.path.isdir('C:\\Users\\Python')
True
>>> os.path.isfile('C:\\Users\\Python')
False
>>> os.path.isdir('C:\\Users\\Python\\calc.exe')
False
>>> os.path.isfile('C:\\Users\\Python\\calc.exe')
True
We can determine whether there is a DVD or flash drive currently attached to the computer by checking for it with the os.path.exists() function. For instance, if I wanted to check for a flash drive with the volume named D:\ on my Windows computer, I could do that with the following:
>>> os.path.exists('D:\\')
False
These were some of the commonly used functions from the os.path module but if your interested to explore os.path module in details, the full documentation for the os.path module is on the Python website at https://docs.python.org/3/library/os.path.html
Till we meet next, keep practicing and learning Python as Python is easy to learn!
0 comments:
Post a Comment