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