[英]Python 2.7 is giving me inexplicable “syntax errors”
I wrote a script in python3.3 to sync two directories on windows which worked perfectly; 我在python3.3中编写了一个脚本来同步Windows上的两个目录,这些目录完美无缺; later I wanted to make it an executable, and so I ported it to python 2.7 (cx_freeze gives me "module not found" errors).
后来我想把它变成一个可执行文件,所以我把它移植到python 2.7(cx_freeze给我“模块未找到”错误)。 I'm running into this one weird problem though, which is whenever I input a file path to my program it gives me an error;
我遇到了这个奇怪的问题,这就是每当我输入一个文件路径到我的程序时它会给我一个错误; for example if I put in "C:\\Users\\Me\\SourceDir" the program breaks, outputting
例如,如果我输入“C:\\ Users \\ Me \\ SourceDir”程序中断,输出
File "<string>", line 1
C:\Users\Me\SourceDir
^
Syntax Error: Invalid Syntax
I'm confused as to why this is. 我很困惑为什么会这样。 The program runs fine under python3.3 (with the same paths, I should add), which leads me to believe that there's something weird going on with 2.7 behind the scenes.
程序在python3.3下运行正常(我应该添加相同的路径),这让我相信在幕后有一些奇怪的东西。
Can anyone explain to me why this happens and how to fix it? 任何人都可以向我解释为什么会发生这种情况以及如何解决它?
Also here's the program's source, although I dont feel like it really matters 这也是该程序的来源,虽然我觉得它不重要
'''
Created on Mar 18, 2013
@author: pipsqueaker
'''
import os, shutil, time
from datetime import datetime
class mainClass():
def __init__(self):
self.srcDir = []
self.dst = []
self.iteration = 1
self.fileHash = {}
self.slash = ""
def getParentDir(self, string, slash):
slashCount = 0
tempCount = 0
realDir = ""
for x in string:
if x == "/" or x == "\\":
slashCount += 1
for y in string:
if y == "/" or y == "\\":
tempCount += 1
if tempCount < slashCount:
realDir += y
else:
break
realDir += slash
return realDir
def initializeDirs(self):
#Initialize Paths from the setup files
onWindows = (os.name == 'nt')
if onWindows:
self.slash = "\\"
else:
self.slash = "/"
os.chdir(self.getParentDir(os.path.realpath(__file__), self.slash))
if os.path.exists("srcDir") == False:
print("The srcDir file does not exist; Creating Now...")
self.srcDir = input("Please input source directory \n")
self.newSource = open("srcDir", "w")
if self.srcDir[self.srcDir.__len__() -1] != self.slash:
self.srcDir += self.slash
self.newSource.write(self.srcDir)
self.newSource.close()
if os.path.exists("dstDirs") == False:
print("The dstFirs file does not exits; Creating Now...")
print("Input a directory to sync to. Or just type xit to exit")
self.newDst = open("dstDirs", "w")
while True:
self.IN = input()
if os.name == 'nt': #Windows
self.IN.replace("/", "\\")
else:
self.IN.replace("\\", "/")
if self.IN != "xit":
if self.IN[self.IN.__len__() -1] != self.slash:
self.IN += self.slash
self.newDst.write(self.IN)
self.dst.append(self.IN)
else:
self.newDst.close()
break
self.srcDir = open("srcDir", "r")
self.srcDir = self.srcDir.readline()
self.dstDirs = open("dstDirs", "r")
for line in self.dstDirs:
self.dst.append(line)
def fileHashes(self):
self.fileHash = {}
for file in os.listdir(self.srcDir):
self.fileHash[file] = os.path.getmtime(self.srcDir+file)
def loopForever(self):
print("Filesync Version 1.0 by pipsqueaker \n")
while True:
print("Iteration ", self.iteration, " @ ", datetime.now()) #APPROVE
for destination in self.dst:
for checkFile in os.listdir(destination):
if not os.path.exists(self.srcDir+checkFile):
os.remove(destination+checkFile)
print(checkFile, " removed from ", destination, " @", datetime.now())
self.fileHashes()
for file in os.listdir(self.srcDir):
if os.path.exists(destination+file):
try:
if os.path.getmtime(self.srcDir+file) != self.fileHash[file]:
try:
shutil.copy2((self.srcDir+file), destination)
print(file," was updated to ",destination," @",datetime.now())
except:
pass
except KeyError:
continue
else:
try:
shutil.copy2((self.srcDir+file), destination)
print(file, " was copied to ", destination, " @", datetime.now())
self.fileHashes()
except:
pass
self.iteration += 1
time.sleep(10)
def main(self):
self.initializeDirs()
self.fileHashes()
self.loopForever()
n = mainClass()
n.main()
You are using input()
in the script: 您在脚本中使用
input()
:
self.IN = input()
Python 2 has raw_input()
for this. Python 2为此提供了
raw_input()
。 input()
in python 2 is the same as eval(raw_input())
. python 2中的
input()
与eval(raw_input())
。 Trying to evaluate a path will sure result in a syntax error. 尝试评估路径肯定会导致语法错误。
hello
as input will result in syntax error (unless you have a variable called hello
in scope). hello
作为输入将导致语法错误(除非你在范围内有一个名为hello
的变量)。 "hello"
will indeed evaluate to the string "hello". "hello"
的确会评估字符串“你好”。 just like simple expressions in python. 就像python中的简单表达式一样。
You may want to take a look at the 2to3 tool . 您可能需要查看2to3工具 。
i think the problem is youre using input
when you should be using raw_input
我认为问题是你在使用
raw_input
时使用input
alot of people get these confused when converting programs to 2.7 在将程序转换为2.7时,很多人都会感到困惑
in 2.7 input takes ints and raw_input
will take a string or an int and turn it tinto a string 在2.7输入中取内联,
raw_input
将取一个字符串或一个int并将其转换为字符串
this line: 这一行:
self.srcDir = input("Please input source directory \n")
should be: 应该:
self.srcDir = raw_input("Please input source directory \n")
that should fix it 应该修复它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.