2011-12-07 23 views
1

Python 2.7using python 2.7:sh:-c:line 0:語法錯誤附近的意外標記`('

我收到以下錯誤消息。 。運氣相當基本的腳本有我們所有的Windows環境

#!/usr/bin/env python 

import os 
import time 
import subprocess 
import platform 

#Log Files 

source = ['"/home/datatec/ds/datos"' ] 

#Backup 
target_dir = "/home/datatec/Backup" 

#Zip file + date and time 
target = "target_dir + os.sep + time.strftime('%Y%m%d') + platform.node() + '.zip'" 

zip_command = zip_command = "zip -r {0} {1}".format(target, ' '.join(source)) 
err = open('error.txt' , 'w') 

#Run the backup + verify 
if os.system(zip_command) == 0: 

    print('Successful backup to', target) 
else: 
    print('Backup FAILED') 

#create server directory, all folders are moved at end of the week 
mkdir = 'ssh [email protected] "cd ../proddata;sleep 3;hn=$ushsdtec01p;mkdir ushsdtec01p;"' 
os.system(mkdir) 

dz = "rm /home/datatec/Backup/*.zip" 
psfiles = "scp *.zip [email protected]:/proddata/ushsdtec01p/" 
print 'TRANSFERRING ZIP FILES!!' 
if os.system(psfiles) == 0: 
    os.system(dz) 
    print ('Files transferred') 
else: 
     print('TRANSFER FAILED') 
     err.write("job failed") 
     err.close() 

工作是這樣的輸出:

sh: -c: line 0: syntax error near unexpected token `(' 
sh: -c: line 0: `zip -r target_dir + os.sep + time.strftime('%Y%m%d') + platform.node() + '.zip' "/home/datatec/ds/datos"' 
Backup FAILED 

回答

2

對於初學者來說,「TARGET = ...」行可能不應該被引用。

3

os.system在子shell中執行參數,其中大括號具有特殊含義。您應該使用subprocess

不過,現實的錯誤是由引號

target = "target_dir + os.sep + time.strftime('%Y%m%d') + platform.node() + '.zip'" 

此行引起幾乎可以肯定應該是

target = target_dir + os.sep + time.strftime('%Y%m%d') + platform.node() + '.zip' 
+1

乾杯!感謝您幫助newb。希望我能儘快把這個恩惠歸還給別人! – user1086526

相關問題