[英]using python 2.7 : sh: -c: line 0: syntax error near unexpected token `('
Python 2.7 Python 2.7
I am getting the following error message. 我收到以下错误消息。 Not clear as to why this is.
不清楚为什么会这样。 I have searched and no luck.
我搜索了,没有运气。 Fairly basic script.
基本的脚本。 Has worked across all our windows environments
已经在我们所有的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 logcp@ushsdata01p "cd ../proddata;sleep 3;hn=$ushsdtec01p;mkdir ushsdtec01p;"'
os.system(mkdir)
dz = "rm /home/datatec/Backup/*.zip"
psfiles = "scp *.zip logcp@ushsdata01p:/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()
this is the output: 这是输出:
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
os.system
executes an argument in a subshell, where braces have special meanings. os.system
在子os.system
执行参数,其中括号具有特殊含义。 You should use subprocess
instead. 您应该改用
subprocess
。
But the immediate error is caused by the quotes in 但是立即错误是由引号引起的
target = "target_dir + os.sep + time.strftime('%Y%m%d') + platform.node() + '.zip'"
This line should almost certainly be 这行几乎可以肯定是
target = target_dir + os.sep + time.strftime('%Y%m%d') + platform.node() + '.zip'
对于初学者,可能不应该引用“ target = ...”行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.