gpt4 book ai didi

Python递归文件夹读取

转载 作者:IT老高 更新时间:2023-10-28 12:21:44 25 4
gpt4 key购买 nike

我有 C++/Obj-C 背景,我刚刚发现 Python(已经写了大约一个小时)。我正在编写一个脚本来递归读取文件夹结构中文本文件的内容。

我遇到的问题是我编写的代码仅适用于一个文件夹深处。我可以看到为什么在代码中(参见 #hardcoded path),我只是不知道如何继续使用 Python,因为我对它的体验只是全新的。

Python 代码:

import os
import sys

rootdir = sys.argv[1]

for root, subFolders, files in os.walk(rootdir):

for folder in subFolders:
outfileName = rootdir + "/" + folder + "/py-outfile.txt" # hardcoded path
folderOut = open( outfileName, 'w' )
print "outfileName is " + outfileName

for file in files:
filePath = rootdir + '/' + file
f = open( filePath, 'r' )
toWrite = f.read()
print "Writing '" + toWrite + "' to" + filePath
folderOut.write( toWrite )
f.close()

folderOut.close()

最佳答案

确保你理解os.walk的三个返回值:

for root, subdirs, files in os.walk(rootdir):

有以下含义:

  • root:“遍历”的当前路径
  • subdirs:root 类型目录的文件
  • files:root(不在subdirs)中的非目录类型的文件

请使用 os.path.join 而不是用斜杠连接!你的问题是 filePath = rootdir + '/' + file - 你必须连接当前的“walked”文件夹而不是最上面的文件夹。所以那一定是filePath = os.path.join(root, file)。 BTW "file"是一个内置的,所以你通常不使用它作为变量名。

另一个问题是你的循环,应该是这样的,例如:

import os
import sys

walk_dir = sys.argv[1]

print('walk_dir = ' + walk_dir)

# If your current working directory may change during script execution, it's recommended to
# immediately convert program arguments to an absolute path. Then the variable root below will
# be an absolute path as well. Example:
# walk_dir = os.path.abspath(walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))

for root, subdirs, files in os.walk(walk_dir):
print('--\nroot = ' + root)
list_file_path = os.path.join(root, 'my-directory-list.txt')
print('list_file_path = ' + list_file_path)

with open(list_file_path, 'wb') as list_file:
for subdir in subdirs:
print('\t- subdirectory ' + subdir)

for filename in files:
file_path = os.path.join(root, filename)

print('\t- file %s (full path: %s)' % (filename, file_path))

with open(file_path, 'rb') as f:
f_content = f.read()
list_file.write(('The file %s contains:\n' % filename).encode('utf-8'))
list_file.write(f_content)
list_file.write(b'\n')

如果您不知道,文件的 with 语句是简写:

with open('filename', 'rb') as f:
dosomething()

# is effectively the same as

f = open('filename', 'rb')
try:
dosomething()
finally:
f.close()

关于Python递归文件夹读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com