Working with os.path
The os.path contains several helpful utility function. The most important functions are creating paths, deconstructing paths, expanding user details, testing for path existence and obtaining some information about file properties.
Some most common used os.path functions are:-
you can see this operation in action on your test files and folder
Some most common used os.path functions are:-
- os.path.getsize()
- os.path.getctime()
- os.path.isfile()
- os.path.isdir()
- os.path.splitdrive()
- os.path.splitext()
- os.path.join()
os.path helps with processing paths. You can find the full path to your file using os.path.abspath() and if its a link, the path to the real file with os.path.realpath(). Python full path look like this
[<drive>] <path to folder><filename><extension>
TRY IT OUT
- Change into root folder or create a root folder.
- Place random text files in root folder.
- Fire up python and type following code
>>> import os >>> from os import path >>> os.listdir('.') ['fileA.txt', 'fileB.txt', 'fileX.txt', 'ls.txt', 'subdirectory', 'test5'] >>> path.getsize('fileA.txt') 0 >>> path.getctime('fileA.txt') 1569079319.0 >>> path.abspath('fileA.txt') '/Users/apple/test-project/root/fileA.txt' >>> folder , filename = path.split(path.abspath('fileA.txt')) >>> print(folder) /Users/apple/test-project/root >>> print(filename) fileA.txt >>> path.splitext(filename) ('fileA', '.txt') >>> path.splitext(filename) ('fileA', '.txt') >>> path.join(folder,filename) '/Users/apple/test-project/root/fileA.txt'
Comments
Post a Comment