在 python 中的多个文件中删除一行
Deleting a line in multiple files in python
我是python的初学者,目前正在练习
所以我想要做的是一个脚本,它找到我正在用 raw_input 编写的一行,然后在多个文件中搜索这一行并将其删除。
类似这样但需要更多文件:
word = raw_input("word: ")
f = open("file.txt","r")
lines = f.readlines()
f.close()
f = open("file.txt","w")
for line in lines:
if line!=mail+"\n":
f.write(line)
f.close()
这是一个简单的任务,但实际上对我来说很难,因为我在任何地方都找不到示例。
像这样的东西应该可以工作:
source = '/some/dir/path/'
for root, dirs, filenames in os.walk(source):
for f in filenames:
this_file = open(os.path.join(source, f), "r")
this_files_data = this_file.readlines()
this_file.close()
# rewrite the file with all line except the one you don't want
this_file = open(os.path.join(source, f), "w")
for line in this_files_data:
if line != "YOUR UNDESIRED LINE HERE":
this_file.write(line)
this_file.close()
与其将整个文件读入内存,不如遍历文件并将正确的行写入临时文件。完成整个文件后,将其删除并将临时文件重命名为原始文件的名称。这是您以后很可能会经常遇到的经典模式。
我还建议将其分解为函数。您应该首先编写代码以仅从单个文件中删除所有出现的行。然后你可以编写另一个函数,它简单地遍历文件名列表并调用第一个函数(对单个文件进行操作)。
要获取目录中所有文件的文件名,请使用os.walk
。如果您不想将此功能应用于目录中的所有文件,您可以自己设置 files
变量来存储您想要的任何文件名配置。
import os
def remove_line_from_file(filename, line_to_remove, dirpath=''):
"""Remove all occurences of `line_to_remove` from file
with name `filename`, contained at path `dirpath`.
If `dirpath` is omitted, relative paths are used."""
filename = os.path.join(dirpath, filename)
temp_path = os.path.join(dirpath, 'temp.txt')
with open(filename, 'r') as f_read, open(temp_path, 'w') as temp:
for line in f_read:
if line.strip() == line_to_remove:
continue
temp.write(line)
os.remove(filename)
os.rename(temp_path, filename)
def main():
"""Driver function"""
directory = raw_input('directory: ')
word = raw_input('word: ')
dirpath, _, files = next(os.walk(directory))
for f in files:
remove_line_from_file(f, word, dirpath)
if __name__ == '__main__':
main()
测试
所有这些文件都在同一目录中。左边是 运行 命令之前的样子,右边是命令之后的样子。我输入的 "word" 是 Remove this line
.
a.txt
Foo Foo
Remove this line Bar
Bar Hello
Hello World
Remove this line
Remove this line
World
b.txt
Nothing Nothing
In In
This File This File
Should Should
Be Changed Be Changed
c.txt
Remove this line
d.txt
The last line will be removed The last line will be removed
Remove this line
我是python的初学者,目前正在练习
所以我想要做的是一个脚本,它找到我正在用 raw_input 编写的一行,然后在多个文件中搜索这一行并将其删除。 类似这样但需要更多文件:
word = raw_input("word: ")
f = open("file.txt","r")
lines = f.readlines()
f.close()
f = open("file.txt","w")
for line in lines:
if line!=mail+"\n":
f.write(line)
f.close()
这是一个简单的任务,但实际上对我来说很难,因为我在任何地方都找不到示例。
像这样的东西应该可以工作:
source = '/some/dir/path/'
for root, dirs, filenames in os.walk(source):
for f in filenames:
this_file = open(os.path.join(source, f), "r")
this_files_data = this_file.readlines()
this_file.close()
# rewrite the file with all line except the one you don't want
this_file = open(os.path.join(source, f), "w")
for line in this_files_data:
if line != "YOUR UNDESIRED LINE HERE":
this_file.write(line)
this_file.close()
与其将整个文件读入内存,不如遍历文件并将正确的行写入临时文件。完成整个文件后,将其删除并将临时文件重命名为原始文件的名称。这是您以后很可能会经常遇到的经典模式。
我还建议将其分解为函数。您应该首先编写代码以仅从单个文件中删除所有出现的行。然后你可以编写另一个函数,它简单地遍历文件名列表并调用第一个函数(对单个文件进行操作)。
要获取目录中所有文件的文件名,请使用os.walk
。如果您不想将此功能应用于目录中的所有文件,您可以自己设置 files
变量来存储您想要的任何文件名配置。
import os
def remove_line_from_file(filename, line_to_remove, dirpath=''):
"""Remove all occurences of `line_to_remove` from file
with name `filename`, contained at path `dirpath`.
If `dirpath` is omitted, relative paths are used."""
filename = os.path.join(dirpath, filename)
temp_path = os.path.join(dirpath, 'temp.txt')
with open(filename, 'r') as f_read, open(temp_path, 'w') as temp:
for line in f_read:
if line.strip() == line_to_remove:
continue
temp.write(line)
os.remove(filename)
os.rename(temp_path, filename)
def main():
"""Driver function"""
directory = raw_input('directory: ')
word = raw_input('word: ')
dirpath, _, files = next(os.walk(directory))
for f in files:
remove_line_from_file(f, word, dirpath)
if __name__ == '__main__':
main()
测试
所有这些文件都在同一目录中。左边是 运行 命令之前的样子,右边是命令之后的样子。我输入的 "word" 是 Remove this line
.
a.txt
Foo Foo
Remove this line Bar
Bar Hello
Hello World
Remove this line
Remove this line
World
b.txt
Nothing Nothing
In In
This File This File
Should Should
Be Changed Be Changed
c.txt
Remove this line
d.txt
The last line will be removed The last line will be removed
Remove this line