2016-11-13 18 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" 

所以反斜槓不會被解釋。但有一個例外:如果字符串以反斜槓結尾,您仍然需要加倍。

相關問題