如何根据 python 的某些字符串删除 TXT 文件中的某些行,将文件内容复制到另一个文件
how to copy content of file to another file with deleting some lines in a TXT file based on some strings with python
我有这个 python 脚本可以打开文件对话框和 select 文本文件而不是将其内容复制到另一个文件,在复制到下一个文件之前我需要做的是根据脚本中预定义的一些字符串删除几行。
问题是文件原样复制,没有删除任何内容。
谁能帮我解决这个问题??
OPenDirectory.py
#!/usr/bin/python
import Tkinter
from os import listdir
from os.path import isfile, join
import tkFileDialog
def readWrite():
unwanted = set(['thumbnails', 'tikare', 'cache'])
mypath = "C:\Users\LT GM\Desktop/"
Tkinter.Tk().withdraw()
in_path = tkFileDialog.askopenfile(initialdir = mypath, filetypes=[('text files', ' TXT ')])
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in files:
if file.split('.')[1] == 'txt':
outputFileName = 'Sorted-' + file
with open(mypath + outputFileName, 'w') as w:
with open(mypath + '/' + file) as f:
for l in f:
if l != unwanted:
print l
w.write(l)
in_path.close()
if __name__== "__main__":
readWrite()
ChangedScipt
#!/usr/bin/python
import Tkinter
from os import listdir
from os.path import isfile, join
import tkFileDialog
def readWrite():
unwanted = set(['thumbnails', 'tikare', 'cache'])
mypath = "C:\Users\LT GM\Desktop/"
Tkinter.Tk().withdraw()
in_path = tkFileDialog.askopenfile(initialdir = mypath, filetypes=[('text files', ' TXT ')])
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in files:
if file.split('.')[1] == 'txt':
if file.strip() in unwanted:
continue
outputFileName = 'Sorted-' + file
with open(mypath + outputFileName, 'w') as w:
with open(mypath + '/' + file) as f:
for l in f:
if l != unwanted:
print l
w.write(l)
in_path.close()
if __name__== "__main__":
readWrite()
blacklist = set(['thumbnails', 'quraan', 'cache'])
with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
for line in infile:
if line.strip() in blacklist: continue # if you want to match the full line
# if any(word in line for word in blacklist): continue # if you want to check if a word exists on a line
outfile.write(line)
我有这个 python 脚本可以打开文件对话框和 select 文本文件而不是将其内容复制到另一个文件,在复制到下一个文件之前我需要做的是根据脚本中预定义的一些字符串删除几行。
问题是文件原样复制,没有删除任何内容。
谁能帮我解决这个问题??
OPenDirectory.py
#!/usr/bin/python
import Tkinter
from os import listdir
from os.path import isfile, join
import tkFileDialog
def readWrite():
unwanted = set(['thumbnails', 'tikare', 'cache'])
mypath = "C:\Users\LT GM\Desktop/"
Tkinter.Tk().withdraw()
in_path = tkFileDialog.askopenfile(initialdir = mypath, filetypes=[('text files', ' TXT ')])
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in files:
if file.split('.')[1] == 'txt':
outputFileName = 'Sorted-' + file
with open(mypath + outputFileName, 'w') as w:
with open(mypath + '/' + file) as f:
for l in f:
if l != unwanted:
print l
w.write(l)
in_path.close()
if __name__== "__main__":
readWrite()
ChangedScipt
#!/usr/bin/python
import Tkinter
from os import listdir
from os.path import isfile, join
import tkFileDialog
def readWrite():
unwanted = set(['thumbnails', 'tikare', 'cache'])
mypath = "C:\Users\LT GM\Desktop/"
Tkinter.Tk().withdraw()
in_path = tkFileDialog.askopenfile(initialdir = mypath, filetypes=[('text files', ' TXT ')])
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in files:
if file.split('.')[1] == 'txt':
if file.strip() in unwanted:
continue
outputFileName = 'Sorted-' + file
with open(mypath + outputFileName, 'w') as w:
with open(mypath + '/' + file) as f:
for l in f:
if l != unwanted:
print l
w.write(l)
in_path.close()
if __name__== "__main__":
readWrite()
blacklist = set(['thumbnails', 'quraan', 'cache'])
with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
for line in infile:
if line.strip() in blacklist: continue # if you want to match the full line
# if any(word in line for word in blacklist): continue # if you want to check if a word exists on a line
outfile.write(line)