简体   繁体   English

使用python 2.7:sh:-c:第0行:意外令牌'('附近的语法错误

[英]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.

相关问题 在 subprocess.call(cmd, ...) 中运行 python 得到错误消息 /bin/sh: -c: line 0: syntax error near unexpected token `(' - Running python in subprocess.call(cmd, ...) get error with message /bin/sh: -c: line 0: syntax error near unexpected token `(' -c:第0行:意外令牌'('附近的语法错误 - -c: line 0: syntax error near unexpected token `(' 意外标记'('python脚本附近的语法错误 - syntax error near unexpected token `(' python script 意外令牌附近的Python语法错误 - Python syntax error near unexpected token Python:-bash:意外令牌附近的语法错误 - Python: -bash: syntax error near unexpected token bash:意外标记“(”附近的语法错误 - Python - bash: syntax error near unexpected token `(' - Python 在意外令牌附近获取语法错误`;' 在python中 - getting syntax error near unexpected token `;' in python bash:-c:第0行:从python-os.system内部运行linux cmd时,意外令牌'newline'附近的语法错误 - bash: -c: line 0: syntax error near unexpected token `newline' when running a linux cmd from inside python-os.system bash:Python数字游戏中意外标记“ newline”附近的语法错误 - bash: syntax error near unexpected token `newline' in Python Number game alchemyAPI python sdk中意外令牌'('附近的语法错误 - Syntax Error near unexpected token `(' in alchemyAPI python sdk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM