-
-
Save ltaoist/187bcfc4b340fcd2c1a379e7ae938a3f to your computer and use it in GitHub Desktop.
Python 遍历当前目录下的文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 只可以遍历指定文件夹rootdir中的文件夹名和文件名 | |
# 无法遍历出其中包含的子文件夹中的内容 | |
import os | |
rootdir = './' # 需要遍历的文件夹,这里设定为当前文件夹 | |
list = os.listdir(rootdir) | |
for line in list: | |
filepath = os.path.join(rootdir, line) | |
if os.path.isdir(filepath): | |
print "dir:" + filepath | |
if os.path.isfile(filepath): | |
print "file:" + filepath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 遍历指定文件夹rootdir中的所有文件夹及文件,包括子文件夹 | |
import os | |
rootdir = './' # 需要遍历的文件夹,这里设定为当前文件夹 | |
# 如果此循环在迭代第一次时break出,则其效果和上面的相同 | |
for root, dirs, files in os.walk(rootdir): # 当前路径、子文件夹名称、文件列表 | |
for filename in files: | |
print filename | |
for dirname in dirs: | |
print dirname |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment