p = "192.168.12.12"
response = os.system("ping " + ip + " -n 1")
if response == 0:
print("System is UP !")
else:
print("System is DOWN !")
source = input("Enter source file with full path: ")
target = input("Enter target file with full path: ")
target1 = '\\192.168.62.53\d$\cop1.txt'
try:
copyfile("f:\cop1.txt",target1)
except IOError as e:
print("Unable to copy file. %s" % e)
exit(1)
except:
print("Unexpected error:", sys.exc_info())
exit(1)
print("\nFile copy done!\n")
我在此代码中的问题是,当应用运行编译显示错误时,我未输入hard_code(如target1
)中的地址时输入
(没有这样的文件或目录:'\ 192.168.62.53 \ d $ \ cop1.txt')
但是当我在运行中输入地址时,程序将完全运行并复制文件。
出什么问题了?
答案 0 :(得分:1)
您需要用另一个反斜杠转义反斜杠,就像(无意间)对错误消息中打印的一个反斜杠一样。
target1 = '\\\\192.168.62.53\\d$\\cop1.txt'
另一种选择是使用三引号:
target1 = """\\192.168.62.53\d$\cop1.txt"""
此外,最好使用os.path.join并让python创建正确的路径。