2017-06-14 108 views
0

該代碼讀取目錄中的文件,並用由python生成的隨機6位數字替換字符串「400432」,構建腳本並執行。Python用多個文件中的隨機數字替換字符串

每個文件應該有一個唯一的隨機6位數與相應的腳本。文件1:123456和腳本123456.sh,文件2:775463和腳本775463.sh等

問題是當存在多個文件時123456被寫入所有文件並且只創建123456.sh。我不知道如何循環這一點,距離我使用python已經有3年了。

努力完成這一點。謝謝你的幫助!

#!/usr/bin/python 

import os 
import stat 
import fileinput 
import glob 
import sys 
import re 
import random 
import subprocess 
import string 


############################################################## 
# Generates random numbers and replaces in WSP files 
############################################################## 

def random_digits(y): 
    return ''.join(random.choice(string.digits) for x in range(y)) 
rand = str(random_digits(6)) 
_replace_re = re.compile("400432") 
for dirpath, dirnames, filenames in os.walk("/home/mark/WSP_IN/"): 
    for file in filenames: 
     file = os.path.join(dirpath, file) 
     head, tail = os.path.split(file) 
    tempfile = file + ".temp" 
     with open(tempfile, "w") as target: 
      with open(file) as source: 
       for line in source: 
        line = _replace_re.sub(rand, line) 
        target.write(line) 
      os.rename(tempfile, file) 
############################################################## 
# Creates script that builds ship to based on random numbers 
############################################################## 

s = open('/usr/local/bin/wsp_scripts/' + rand + '.sh', 'wb+') 

############################################################## 
# Define responses as string arguments rather than text 
############################################################## 

data = '/usr/local/bin/wsp_scripts/' 
ext = '.sh' 
space = ' '; 
script = 'exec ${B}/pro5 -c${CF}' 
pathx = ' -m2048 -q ${P} - ' 
swfile = str(tail+space) 
swship = str(rand) 
sscript = str(script) 
spathx = str(pathx) 
shell = str(data+rand+ext) 

############################################################## 
# Write static text and arguments into file 
############################################################## 

s.write('#!/bin/bash' + '\n') 
s.write('#termsoa=/soatermsystem' + '\n'), 
s.write('PATH=$PATH:/usr/basic:/usr/basic/util' + '\n') 
s.write('B=/usr/local/basis/pro5' + '\n') 
s.write('P=/usr5/prog/utils/SO/EDIshipbuild' + '\n') 
s.write('CF=/usr/local/basis/pro5/config.bbx'+ '\n') 
s.write('TERMCAP=${B}/termcap' + '\n') 
s.write('export PATH TERM TERMCAP' + '\n') 
s.write('umask 0' + '\n') 
s.write('cd /usr/basic' + '\n') 
s.write(sscript+spathx+swfile+rand) 

st = os.stat(shell) 
os.chmod(shell, st.st_mode | stat.S_IEXEC) 
s.close() 

############################################################## 
# Import script just created and execute 
############################################################## 

cmd = '/usr/local/bin/wsp_scripts/' + rand + '.sh' 

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 
out, err = p.communicate() 
result = out.split('\n') 
for lin in result: 
    if not lin.startswith('#'): 
     print(lin) 
s.close() 

回答

0

您定義randfor循環之外創建sh文件。如果要爲每個文件重新定義rand,請將其定義移到inner for循環中。 .sh文件創建也一樣。

我認爲這看起來像這樣。

def random_digits(y): 
    return ''.join(random.choice(string.digits) for x in range(y)) 

_replace_re = re.compile("400432") 

for dirpath, dirnames, filenames in os.walk("/home/mark/WSP_IN/"): 
    for file in filenames: 
     rand = str(random_digits(6)) 
     file = os.path.join(dirpath, file) 
     head, tail = os.path.split(file) 
     tempfile = file + ".temp" 
     with open(tempfile, "w") as target: 
      with open(file) as source: 
       for line in source: 
        line = _replace_re.sub(rand, line) 
        target.write(line) 
     os.rename(tempfile, file) 
     ############################################################## 
     # Creates script that builds ship to based on random numbers 
     ############################################################## 

     s = open('/usr/local/bin/wsp_scripts/' + rand + '.sh', 'wb+') 
+0

這適用於修改文件並用唯一編號替換文本,腳本也是唯一的。但是,只有一個腳本被寫入,另一個是空白的。我必須需要另一個循環s.write? – Mark

+0

您需要在相同縮進級別打開後立即添加寫入。 –

+0

謝謝Stephen!除了腳本一次全部運行,我需要它們按順序運行或在每個腳本完成後運行。 – Mark

0

我不知道,如果使用6位數字是一個要求或不是?

import time 
def random_digits(y): 
    return int(time.time()) 

它將返回10位數的時間戳,並且將始終是唯一的。

讓我知道,如果有幫助!

相關問題