python - How to find files and skip directories in os.listdir
文章推薦指數: 80 %
You need to filter out directories; os.listdir() lists all names in a given path. You can use os.path.isdir() for this: basepath = '/path/to/directory' for ...
WelcometoOStackKnowledgeSharingCommunityforprogrammeranddeveloper-Open,LearningandShare
Login
Remember
Register
Ask
Q&A
AllActivity
Hot!
Unanswered
Tags
Categories
Users
AskaQuestion
AskaQuestion
Categories
Allcategories
Topic[话题](3)
Life[生活](4)
Technique[技术](86.6k)
Idea[创意](2)
Jobs[工作](0)
Others[杂七杂八](11)
python-Howtofindfilesandskipdirectoriesinos.listdir
0votes
72views
asked
Oct17,2021
by
深蓝
(31.9mpoints)
python-Howtofindfilesandskipdirectoriesinos.listdir
Iuseos.listdiranditworksfine,butIgetsub-directoriesinthelistalso,whichisnotwhatIwant:Ineedonlyfiles.
WhatfunctiondoIneedtouseforthat?
Ilookedalsoatos.walkanditseemstobewhatIwant,butI'mnotsureofhowitworks.
Question&Answers:os与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
python
Pleaseloginorregistertoaddacomment.
Pleaseloginorregistertoanswerthisquestion.
1Answer
0votes
answered
Oct17,2021
by
深蓝
(31.9mpoints)
Youneedtofilteroutdirectories;os.listdir()listsallnamesinagivenpath.Youcanuseos.path.isdir()forthis:
basepath='/path/to/directory'
forfnameinos.listdir(basepath):
path=os.path.join(basepath,fname)
ifos.path.isdir(path):
#skipdirectories
continue
Notethatthisonlyfiltersoutdirectoriesafterfollowingsymlinks.fnameisnotnecessarilyaregularfile,itcouldalsobeasymlinktoafile.Ifyouneedtofilteroutsymlinksaswell,you'dneedtousenotos.path.islink()first.
OnamodernPythonversion(3.5ornewer),anevenbetteroptionistousetheos.scandir()function;thisproducesDirEntry()instances.Inthecommoncase,thisisfasterasthedirentryloadedalreadyhascachedenoughinformationtodetermineifanentryisadirectoryornot:
basepath='/path/to/directory'
forentryinos.scandir(basepath):
ifentry.isdir():
#skipdirectories
continue
#useentry.pathtogetthefullpathofthisentry,oruse
#entry.nameforthebasefilename
Youcanuseentry.is_file(follow_symlinks=False)ifonlyregularfiles(andnotsymlinks)areneeded.
os.walk()doesthesameworkunderthehood;unlessyouneedtorecursedownsubdirectories,youdon'tneedtouseos.walk()here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Pleaseloginorregistertoaddacomment.
Relatedquestions
0votes
70views
1answer
python-Whatdoes-1meaninthiscode([fileforfileinos.listdir(folder_dir)iffile.find("xlsx")!=-1])?
Howcanyouinterpretthecode?[fileforfileinos.listdir(folder_dir)iffile.find("xlsx")!=-1]Also,what...1-mean-in-this-codefile-for-file-in-os-listdirfolder-dir-if-file...
asked
Oct7,2021
by
深蓝
(31.9mpoints)
python
0votes
137views
1answer
python-Whatdoes-1meaninthiscode([fileforfileinos.listdir(folder_dir)iffile.find("xlsx")!=-1])?
Howcanyouinterpretthecode?[fileforfileinos.listdir(folder_dir)iffile.find("xlsx")!=-1]Also,whatdoes-1meaninthiscode?...
asked
Jan27,2021
inTechnique[技术]
by
深蓝
(31.9mpoints)
python
0votes
430views
1answer
python-Whyispandasread_csvabletofindfilebutos.listdirisn't?
Ihaveafileat~/GoogleDrive/iNat/full.csv.iNatPATH=r"~/GoogleDrive/iNat/"FILENAME=iNatPATH+"full.csv"I...path):>>>os.getcwd()...'/Users/
延伸文章資訊
- 1List All Files in A Directory with Python Guideline - Holistic SEO
- 2Get a filtered list of files in a directory - python - Stack Overflow
If there is no built-in method for this, I am currently thinking of writing a for loop to iterate...
- 3How To List Files In Directory Python- Detailed Guide - Stack ...
os.listdir() lists all the files and folders in the directory. ... filter only the file type entr...
- 4Python: Get list of files in directory sorted by size - thisPointer
- 5python - How to find files and skip directories in os.listdir
You need to filter out directories; os.listdir() lists all names in a given path. You can use os....