程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python directory, file related operations

編輯:Python

python Catalog , Document related operations

  • One 、 Get path
  • Two 、 Filter file names
  • 3、 ... and 、 Judge documents / Catalog
  • Four 、 Get the file name

When training models, it is often necessary to deal with data files , Summary here in real time

One 、 Get path

function effect os.path.abspath(__ file__) Returns the absolute path of the current file , You can also specify the absolute path of the path os.path.dirname(path) Return the upper level path of the path os.path.join() Path splicing is possible os.listdir(path) Get all the file names under this path ( Note that only the current directory )

Two 、 Filter file names

function effect filter() Filter file names that meet the criteria

Example ( Select... From the following sequence .jpg picture ):

img_names=['alexnet-owt-4df8aa71.pth', 'Golden Retriever from baidu.jpg',
'imagenet1000.json', 'imagenet_classnames.txt', 'test',
'tiger cat.jpg', 'train']
img_names = list(filter(lambda x:x.endswith('.jpg'),img_names))
print(iimg_names)
result :
>>['Golden Retriever from baidu.jpg', 'tiger cat.jpg']

3、 ... and 、 Judge documents / Catalog

function effect os.path.exists(path) Determine if the path exists os.path.isdir(path) Determine whether it is a folder os.path.isfile(path) Determine if it's a document

Four 、 Get the file name

function effect os.path.basename(path) Get the last file name of the directory
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved