Python 将所选行从不同目录中的文件复制到另一个文件
我有一个包含许多子目录的目录,其中包含文件。我想在“App_integrations”文件夹中打开以“root.vrpj”或“root.vprj”结尾的文件,并将包含单词“table”的行复制到另一个文件中 到目前为止,我一直使用以下代码访问每个文件:Python 将所选行从不同目录中的文件复制到另一个文件,python,python-2.7,copy,file-copying,Python,Python 2.7,Copy,File Copying,我有一个包含许多子目录的目录,其中包含文件。我想在“App_integrations”文件夹中打开以“root.vrpj”或“root.vprj”结尾的文件,并将包含单词“table”的行复制到另一个文件中 到目前为止,我一直使用以下代码访问每个文件: for root, dirs, files in os.walk(movedir): for filename in files: if filename.endswith(("root.vrpj", "root.vprj")): 问题
for root, dirs, files in os.walk(movedir):
for filename in files:
if filename.endswith(("root.vrpj", "root.vprj")):
问题是,我现在拥有的只是我想访问的文件的名称,我被困在这里。您可以尝试以下方法:
f = open('final_file.txt', 'w')
for root, dirs, files in os.walk(movedir):
for filename in files:
if filename.endswith("root.vrpj") or filename.endswith("root.vprj"):
with open(filename) as data:
for line in data:
if "table" in data:
f.write('{}\n'.format(data))
f.close()
这是Ajax代码的一个版本,它关闭了在循环中打开的文件(并修复了几个其他小问题): 但是,当您看到8级缩进时,您需要重构,例如:
def find_files(startdir, *extensions):
for root, dirs, files in os.walk(movedir):
for filename in files:
if filename.endswith(extensions):
yield os.path.join(root, filename)
def find_lines(fname, text):
with open(fname) as fp:
return [line for line in fp if text in line]
with open('final_file.txt', 'w') as f:
for fname in find_files(movedir, 'root.vrpj', 'root.vprj'):
f.writelines(find_lines(fname, 'table'))
我终于解决了
import os
rootdir = my root folder
# creates a file f that contains all the lines of the files
# with "root.vrpj" or "root.vprj" in their name
# and who are inside "App_integrations" folders
# without duplicates
#creating the big file with all the file containing the lines I need
f = open('final_file.txt', 'a')
for root, dirs, files in os.walk(rootdir):
for filename in files:
if (filename.endswith(("root.vrpj", "root.vprj")) and ("App_Integration" in os.path.join(root, filename))):
full_name = os.path.join(root, filename)
data = open(full_name).read()
f.write(data + "\n")
f.close()
#copying the lines I need to f1 without duplicates
lines_seen = set()
f = open('final_file.txt')
f1 = open('testread1.txt', 'a')
doIHaveToCopyTheLine=False
for line in f.readlines():
if (("Table" in line) and (line not in lines_seen)):
doIHaveToCopyTheLine=True
if doIHaveToCopyTheLine:
f1.write(line)
lines_seen.add(line)
f1.close()
f.close()
查找文件
写入输出
def save_行(行,输出文件=sys.std_out):
对于行中的行:
输出_文件。写入(行)
使用路径()。将('w')作为输出文件打开:
保存_行(筛选_行(文件),作为输出_文件)
你可以用打开文件,用open(“filename”,“r”)作为readFile
打开文件,然后用迭代文件中的行,用作为readFile中的行
这可能会让你更进一步,快乐编码:)你应该尽可能提倡使用和
,这会使代码更干净。别忘了外部代码您有很多级联语句。如果有任何行包含“table”
…不起作用,您也将整个输入文件写入f
。我看到的一个问题是,您没有使用os.path.join,为了获得要读取的文件的完整路径(这似乎是必要的),我正在尝试第一个版本。最后的_file.txt是空的。那么您肯定应该尝试第二个版本,因为您不想要8级缩进的原因之一是它使调试变得非常困难。对于v2,您可以检查list(find_files(…)
是否返回您期望的文件,如果find_lines(fname,'table')
,您知道在fname
上有一行包含“table”
,则返回包含该行/这些行的列表。
import os
rootdir = my root folder
# creates a file f that contains all the lines of the files
# with "root.vrpj" or "root.vprj" in their name
# and who are inside "App_integrations" folders
# without duplicates
#creating the big file with all the file containing the lines I need
f = open('final_file.txt', 'a')
for root, dirs, files in os.walk(rootdir):
for filename in files:
if (filename.endswith(("root.vrpj", "root.vprj")) and ("App_Integration" in os.path.join(root, filename))):
full_name = os.path.join(root, filename)
data = open(full_name).read()
f.write(data + "\n")
f.close()
#copying the lines I need to f1 without duplicates
lines_seen = set()
f = open('final_file.txt')
f1 = open('testread1.txt', 'a')
doIHaveToCopyTheLine=False
for line in f.readlines():
if (("Table" in line) and (line not in lines_seen)):
doIHaveToCopyTheLine=True
if doIHaveToCopyTheLine:
f1.write(line)
lines_seen.add(line)
f1.close()
f.close()
from pathlib import Path
import itertools
source_dir = Path(<source_dir>)
patterns = ['**/*root.vrpj', '**/*root.vprj']
files = itertools.chain.from_iterables(source_dir.glob(pat) for pat in patterns))
def filter_lines(files):
for file in files:
if not 'App_Integration' in file.parts:
continue
with file.open('r') as file_handle:
for line in file_handle:
if 'table' in line:
yield line
def save_lines(lines, output_file=sys.std_out):
for line in lines:
output_file.write(line)
with Path(<output_file>).open('w') as output_file:
save_lines(filter_lines(files), as output_file)