xxxxxxxxxx
import os
print('File name : ', os.path.basename(__file__))
print('Directory Name: ', os.path.dirname(__file__))
xxxxxxxxxx
from pathlib import Path
for txt_path in Path("/path/folder/directory").glob("*.txt"):
print(txt_path)
xxxxxxxxxx
# Basic syntax:
import glob
list_of_files = glob.glob("search_pattern")
# Where:
# - the search pattern can be any glob pattern, e.g. /usr/bin/*py* to get the
# list of files in /usr/bin that contain py
xxxxxxxxxx
(_, _, filenames) = os.walk(mypath).next() #if you are confident that the walk will return at least one value
xxxxxxxxxx
>>> import os
>>> path=os.path.dirname("C:/folder1/folder2/filename.xml")
>>> path
'C:/folder1/folder2'
>>> os.path.basename(path)
'folder2'
xxxxxxxxxx
import os.path
from os import path
def main():
print ("Is it File?" + str(path.isfile('guru99.txt')))
print ("Is it File?" + str(path.isfile('myDirectory')))
if __name__== "__main__":
main()