我是一名新手程序员,所以请原谅我的查询代码不好和简单。我试图通过Python将SQL数据库备份从一台机器转移到另一台机器。我在Windows 7 Professional机器上使用Python 3.6.4版。我将它转移到的机器是Windows 10机器。现在,当我在Python shell中逐行输入以下代码时,它没有任何问题。
import os, pyodbc, shutil
connection = pyodbc.connect(driver = '{ODBC Driver 13 for SQL Server}',
server = 'SERVER',
database = 'DATABASE',
UID = 'USER',
PWD = 'PASS',
autocommit = True)
connection.cursor().execute(
r"backup database DATABASE to disk = 'c:/DB/backup/DATABASE.bak';")
connection.close()
os.system(r'net use \\0.0.0.001\DB\backup /user:DOMAIN\USER PASS')
os.system(r'net use \\0.0.0.100\d\Backups /user:DOMAIN\USER PASS')
file = 'DATABASE.bak'
src = os.path.join('\\\\0.0.0.001','DB','backup', file)
dst = os.path.join('\\\\0.0.0.100','d','Backups')
shutil.move(src, dst)
os.system(r'net use * /delete /y')
然而,当我编译代码(通过pyinstaller)并在Windows 10机器上运行它时,它首先在每个额外的时间运行初始时间和错误。我得到的错误是Windows Error 2 (File Not Found)
。
Traceback (most recent call last):
File "shutil.py", line 544, in move
FileNotFoundError: [WinError 2] The system cannot find the file specified: '\\\\0.0.0.001\\DB\\backup\\DATABASE.bak' -> '\\\\0.0.0.100\\d\\Backups\\DATABASE.bak'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "transfer.py", line 47, in SQL
File "shutil.py", line 558, in move
File "shutil.py", line 257, in copy2
File "shutil.py", line 120, in copyfile
FileNotFoundError: [Errno 2] No such file or directory: '\\\\0.0.0.001\\DB\\backup\\DATABASE.bak'
我很困惑为什么会这样。任何指导都会受到赞赏,所以我可以不停地敲打桌子。
答案 0 :(得分:0)
我可以使用os.system()
调用robocoby
然后使用os.remove()
删除原始文件来解决此问题。奇迹般有效。抱歉shutil()
。