使用Python 2.7时:sh: -c: 第0行:语法错误,接近意外标记`(`
Python 2.7
我遇到了以下错误信息。对于为什么会出现这个错误,我不是很明白。我搜索过,但没有找到解决办法。这个脚本很简单,在我们所有的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()
这是输出结果:
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 个回答
2
首先,"target = ..." 这一行的内容可能不应该加引号。
3
os.system
是用来在一个子进程中执行命令的,这里面的大括号有特别的含义。你应该使用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'