如何在不知道python中每个文件的名称的情况下访问目录中的多个文件
我想访问大约5000个文件并逐个处理它们。有没有办法在不硬编码每个文件名的情况下连续访问每个文件?下面的示例就是来自此如何在不知道python中每个文件的名称的情况下访问目录中的多个文件,python,jupyter-notebook,file-access,Python,Jupyter Notebook,File Access,我想访问大约5000个文件并逐个处理它们。有没有办法在不硬编码每个文件名的情况下连续访问每个文件?下面的示例就是来自此 如果您的目录包含父目录和文件,您可以使用 或者你可以用- 欢迎来到,请大家带上,四处看看,阅读并提供一份。 import os, sys # Open a file path = "/var/www/html/" dirs = os.listdir( path ) # This would print all the files and directories for fi
如果您的目录包含父目录和文件,您可以使用 或者你可以用- 欢迎来到,请大家带上,四处看看,阅读并提供一份。
import os, sys
# Open a file
path = "/var/www/html/"
dirs = os.listdir( path )
# This would print all the files and directories
for file in dirs:
print file
# Example taken from os.walk documentation
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print(root, "consumes", end=" ")
print(sum(getsize(join(root, name)) for name in files), end=" ")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS')
for entry in os.scandir(path):
...