简体   繁体   English

如何使用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

i have this python script that open a file dialog and select a text file than copy its content to another file, what i need to do before i copy to the next file is to delete several lines based on some strings that are predefined in the script.我有这个 python 脚本,它打开一个文件对话框并选择一个文本文件而不是将其内容复制到另一个文件,在复制到下一个文件之前我需要做的是根据脚本中预定义的一些字符串删除几行.

the problem is that the file is copied as it is without deleting anything.问题是文件按原样复制而没有删除任何内容。

can anyone help me to solve this issue??谁能帮我解决这个问题??

OPenDirectory.py打开目录.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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM