如何基于variab复制一个文件

2024-10-02 16:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个python脚本,通过搜索和替换文件中的单词来编辑大量文件。你知道吗

有一个名为:C:\python 3.5/remedy line 1.ahk的原始文件

在原始文档中有一个包含我要替换的词(搜索词)的文件,还有一个文本文件,其中包含我要放入最终文档的新词列表。你知道吗

然后脚本运行并完美运行。然后,根据最终文本文件中的一行创建并命名最终文档(代码从第72行开始)。这样我就可以通过观察来判断最终产品是什么。这个文件最初命名为output = open("C:\python 3.5\output.ahk", 'w'),后来在脚本中根据脚本中的第37行重命名。一切正常。你知道吗

所以剩下的似乎很简单的部分,我似乎不知道是如何采取这一个文件,并将其移动到一个目录,它属于。该目录是基于文件名来源的同一行创建的(代码从第82行开始)。如何简单地将文件移动到脚本创建的目录中,即基于变量(代码从第84行开始),因此文件名基于变量。你知道吗

    import shutil
    #below is where your modified file sits, before we move it into it's own     directory named dst, based on a variable #mainnewdir 
    srcdir = r'C:\python 3.5/'+(justfilename)
    dst = (mainnewdir)+(justfilename)
    shutil.copyfile(src, dst)
  1. 为什么它在代码中用额外的\格式化它?你知道吗
  2. 如果我使用/\斜杠,为什么它似乎没有给我一个错误?你知道吗

下面是整个代码,就像我说的,移动文件的最后一部分不起作用:

    import os
    import linecache
    import sys
    import string
    import re

    ## information/replacingvalues.txt this is the text of the values you         want in your final document
    #information = open("C:\python 3.5\replacingvalues.txt", 'r')
    information = open("C:\python 3.5/replacingvalues.txt", 'r')
    # information = open("C:\Program Files (x86)\Python35-        32\Scripts\Text_Find_and_Replace\information/replacingvalues.txt",

    # Text_Find_and_Replace\Result/output.txt This is the dir and the sum or         final document


    # output = open("C:\python 3.5\output.ahk", 'w')
    createblank = open ("C:\python 3.5/output.ahk", 'w')
    createblank.close()
    output = open("C:\python 3.5\output.ahk", 'w')
    # field = open("C:\Program Files (x86)\Python35-        32\Scripts\Text_Find_and_Replace\Field/values.txt"
    # Field is the file or words you will be replacing
    field = open("C:\python 3.5/values.txt", 'r')


    # modified code for autohot key
    # Text_Find_and_Replace\Test/remedy line 1.ahk is the original doc you want modified
    with open("C:\python 3.5/remedy line 1.ahk", 'r') as myfile:
    inline = myfile.read()
    ## remedy line 1.ahk

    informations = []
    fields = []
    dictionary = {}
    i = 0

    for line in information:
        informations.append(line.splitlines())

    for lines in field:
        fields.append(lines.split())
        i = i + 1;

    if (len(fields) != len(informations)):
        print("replacing values and values have different numbers")
        exit();
    else:
        for i in range(0, i):
            rightvalue = str(informations[i])
            rightvalue = rightvalue.strip('[]')
            rightvalue = rightvalue[1:-1]

            leftvalue = str(fields[i])
            leftvalue = leftvalue.strip('[]')
            leftvalue = leftvalue.strip("'")

            dictionary[leftvalue] = rightvalue

            robj = re.compile('|'.join(dictionary.keys()))
            result = robj.sub(lambda m: dictionary[m.group(0)], inline)

       output.write(result)
        information.close;
        output.close;
        field.close;
        output.close()

    import os
    import linecache
    linecache.clearcache()
    newfilename= linecache.getline("C:\python 3.5/remedy line 1.txt",37)
    filename = ("C:\python 3.5/output.ahk")
    os.rename(filename, newfilename.strip())
    #os.rename(filename, newfilename.strip()+".ahk")
    linecache.clearcache()


    ############## below will create a new directory based on the the word         or words in line 37 of the txt file.

    newdirname= linecache.getline("C:\python 3.5/remedy line 1.txt",37)
    #newpath = r'C:\pythontest\automadedir'
    #below removes the /n ie new line raw assci
    justfilename = (newdirname).strip()
    #below removes the .txt from the rest of the justfilename..
    autocreateddir = (justfilename).strip(".txt")
    # below is an example of combining a string and a variable
    # below makes the variable up that will be the name of the new directory         based on reading line 37 of a text file above

    mainnewdir= r'C:\pythontest\automadedir/'+(autocreateddir)
    if not os.path.exists(mainnewdir):
        os.makedirs(mainnewdir)

        linecache.clearcache()
    # ####################################################
    #below is where your modified file sits, before we move it into it's own         directory named dst, based on a variable #mainnewdir
    srcdir = r'C:\python 3.5/'+(justfilename)
    dst = (mainnewdir)+(justfilename)
    shutil.copyfile(src, dst)

Tags: and文件theimporttxtoutputinformationis
1条回答
网友
1楼 · 发布于 2024-10-02 16:28:28

反斜杠没有自己的想法。你知道吗

当按原样粘贴windows路径并且它们包含\nr\b\x\v\U(python3)(请参阅table here)时,您只是在使用转义序列而没有注意到它。你知道吗

当转义序列不存在时(例如\p),它就工作了。但是当它被知道时,文件名通常是无效的。这就解释了问题的明显随机性。你知道吗

要安全地粘贴窗口路径而不更改/转义路径,只需使用raw前缀:

my_file = r"C:\temp\foo.txt"

所以反斜杠不会被解释。但有一个例外:如果字符串以反斜杠结尾,则仍然必须将其加倍。你知道吗

相关问题 更多 >