2016-11-13 19 views
0

我正在构建一个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) 
+2

您在这里提出了两个问题,一个是关于反斜杠表示和一个关于复制文件的问题,这两个问题都可以通过一些非常基础的研究来解答。另请注意,这不是一个论坛,因此你的大部分内容都不合适。看[问]。 – jonrsharpe

+0

我建议你也使用['os.path.join()'](https://docs.python.org/3/library/os.path.html?highlight=os.path.join#os .path.join)函数来建立文件名。 –

回答

1

反斜杠没有自己的主见。

当您粘贴窗口路径作为 - 是它们含有\nr\b\x\v\U(蟒蛇3),(参考table here为所有的),你仅仅使用转义序列没有注意到它。

当转义序列不存在时(例如\p)它的工作原理。但是当知道文件名通常是无效的。这解释了这个问题显然是随机的。

为了能够安全地贴窗户路径不改变/逃避他们,只是使用原料前缀:

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

所以反斜杠不会被解释。但有一个例外:如果字符串以反斜杠结尾,您仍然需要加倍。

相关问题