python中查看文件名和文件路徑的方法:
>>>importos
>>>url='https://images0.cnblogs.com/i/311516/201403/020013141657112.png'
>>>filename=os.path.basename(url)
>>>filepath=os.path.dirname(url)
>>>filename
'020013141657112.png'
>>>filepath
'https://images0.cnblogs.com/i/311516/201403'
>>>
importos
print(os.path.realpath(__file__))#當前文件的路徑
print(os.path.dirname(os.path.realpath(__file__)))#從當前文件路徑中獲取目錄
print(os.path.basename(os.path.realpath(__file__)))#從當前文件路徑中獲取文件名
print(os.listdir(dirname))#只顯示該目錄下的文件名和目錄名,不包含子目錄中的文件,默認為當前文件所在目錄
importos
#os.walk()遍歷文件夾下的所有文件
#os.walk()獲得三組數據(rootdir,dirname,filnames)
deffile_path(file_dir):
forroot,dirs,filesinos.walk(file_dir):
print(root,end='')#當前目錄路徑
print(dirs,end='')#當前路徑下的所有子目錄
print(files)#當前目錄下的所有非目錄子文件
以上內容為大家介紹了python如何查找文件路徑,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。