浏览 Python 中的所有文件夹
Going through all folders in Python
我想遍历目录中的所有文件夹:
directory\
folderA\
a.cpp
folderB\
b.cpp
folderC\
c.cpp
folderD\
d.cpp
文件夹的名字都知道了。
具体来说,我正在尝试计算每个 a.cpp
、b.cpp
、c.pp
和 d.cpp
源文件的代码行数。因此,进入 folderA
并阅读 a.cpp
,计算行数,然后返回目录,进入 folderB
,阅读 b.cpp
,计算行数等
这是我到目前为止的情况,
dir = directory_path
for folder_name in folder_list():
dir = os.path.join(dir, folder_name)
with open(dir) as file:
source= file.read()
c = source.count_lines()
但我是 Python 的新手,不知道我的方法是否合适以及如何进行。任何显示的示例代码将不胜感激!
此外,with open
是否按应有的方式处理文件 opening/closing 以进行所有这些读取或需要更多处理?
使用Python 3的os.walk()
遍历给定路径的所有子目录和文件,打开每个文件并执行您的逻辑。您可以使用 'for' 循环来遍历它,从而大大简化您的代码。
我会这样做:
import glob
import os
path = 'C:/Users/me/Desktop/' # give the path where all the folders are located
list_of_folders = ['test1', 'test2'] # give the program a list with all the folders you need
names = {} # initialize a dict
for each_folder in list_of_folders: # go through each file from a folder
full_path = os.path.join(path, each_folder) # join the path
os.chdir(full_path) # change directory to the desired path
for each_file in glob.glob('*.cpp'): # self-explanatory
with open(each_file) as f: # opens a file - no need to close it
names[each_file] = sum(1 for line in f if line.strip())
print(names)
输出:
{'file1.cpp': 2, 'file3.cpp': 2, 'file2.cpp': 2}
{'file1.cpp': 2, 'file3.cpp': 2, 'file2.cpp': 2}
关于with
问题,您不需要关闭文件或进行任何其他检查。你现在应该是安全的。
您可以,但是,请检查 full_path
是否存在,因为有人(您)可能会错误地从您的 PC 中删除文件夹(来自 list_of_folders
)
你可以通过 os.path.isdir
which returns True
如果文件存在:
os.path.isdir(full_path)
PS:我用了Python3.
正如 manglano 所说,os.walk()
您可以生成文件夹列表。
[src for src,_,_ in os.walk(sourcedir)]
您可以生成文件路径列表。
[src+'/'+file for src,dir,files in os.walk(sourcedir) for file in files]
我想遍历目录中的所有文件夹:
directory\
folderA\
a.cpp
folderB\
b.cpp
folderC\
c.cpp
folderD\
d.cpp
文件夹的名字都知道了。
具体来说,我正在尝试计算每个 a.cpp
、b.cpp
、c.pp
和 d.cpp
源文件的代码行数。因此,进入 folderA
并阅读 a.cpp
,计算行数,然后返回目录,进入 folderB
,阅读 b.cpp
,计算行数等
这是我到目前为止的情况,
dir = directory_path
for folder_name in folder_list():
dir = os.path.join(dir, folder_name)
with open(dir) as file:
source= file.read()
c = source.count_lines()
但我是 Python 的新手,不知道我的方法是否合适以及如何进行。任何显示的示例代码将不胜感激!
此外,with open
是否按应有的方式处理文件 opening/closing 以进行所有这些读取或需要更多处理?
使用Python 3的os.walk()
遍历给定路径的所有子目录和文件,打开每个文件并执行您的逻辑。您可以使用 'for' 循环来遍历它,从而大大简化您的代码。
我会这样做:
import glob
import os
path = 'C:/Users/me/Desktop/' # give the path where all the folders are located
list_of_folders = ['test1', 'test2'] # give the program a list with all the folders you need
names = {} # initialize a dict
for each_folder in list_of_folders: # go through each file from a folder
full_path = os.path.join(path, each_folder) # join the path
os.chdir(full_path) # change directory to the desired path
for each_file in glob.glob('*.cpp'): # self-explanatory
with open(each_file) as f: # opens a file - no need to close it
names[each_file] = sum(1 for line in f if line.strip())
print(names)
输出:
{'file1.cpp': 2, 'file3.cpp': 2, 'file2.cpp': 2}
{'file1.cpp': 2, 'file3.cpp': 2, 'file2.cpp': 2}
关于with
问题,您不需要关闭文件或进行任何其他检查。你现在应该是安全的。
您可以,但是,请检查 full_path
是否存在,因为有人(您)可能会错误地从您的 PC 中删除文件夹(来自 list_of_folders
)
你可以通过 os.path.isdir
which returns True
如果文件存在:
os.path.isdir(full_path)
PS:我用了Python3.
正如 manglano 所说,os.walk()
您可以生成文件夹列表。
[src for src,_,_ in os.walk(sourcedir)]
您可以生成文件路径列表。
[src+'/'+file for src,dir,files in os.walk(sourcedir) for file in files]