[英]How to execfile file with parameters Python?
I tried to to that like: 我试图这样做:
execfile('c:/xampp/mysql/bin/mysqldump -h localhost -u ' + username + ' -p' + password + ' ' + database + ' > ' + projectname + '.sql');
Then I see an error: 然后我看到一个错误:
IOError: [Errno 2] No such file or directory: 'c:/xampp/mysql/bin/mysqldump -h localhost -u root -p pr > pr.sql' IOError:[Errno 2]没有这样的文件或目录:'c:/ xampp / mysql / bin / mysqldump -h localhost -u root -p pr> pr.sql'
What I do wrong? 我做错了什么?
I trie to write output to file like: 我试图将输出写入文件,如:
output = subprocess.check_output([r'c:\xampp\mysql\bin\mysqldump', '-h', 'localhost', '-u', username, '--password=', database], stdout=open(projectname + '.sql','w'))
It does not work 这是行不通的
execfile doesn't take a whole command, but the name of the file to execute as the first argument. execfile并不需要一个完整的命令,而是要作为第一个参数执行的文件名。 Moreover it's a way of calling other Python program, not a generic program - like mysqldump
. 而且,这是一种调用其他Python程序的方法,而不是像mysqldump
这样的通用程序。
I suggest you use subprocess.call
instead , eg 我建议您改用subprocess.call
,例如
subprocess.call(['c:/xampp/mysql/bin/mysqldump', '-h', 'localhost', '-u', username, '-p' + password, database])
And then you need to take care of the redirection (typically done by your shell if you run it manually), so the full solution would be: 然后,您需要注意重定向(如果手动运行,通常由外壳程序完成),因此完整的解决方案是:
f = open(projectname+".sql", "w")
subprocess.call(['c:/xampp/mysql/bin/mysqldump', '-h', 'localhost', '-u', username, '-p' + password, database], stdout=f)
execfile
is designed to run python files only (the name is misleading). execfile
设计为仅运行python文件(名称具有误导性)。
Plus it has been removed from Python 3... 另外,它已从Python 3中删除...
For your issue, just use subprocess.check_output
, splitting the arguments properly so quoting is handled automatically (never compose your command line by yourself). 对于您的问题,只需使用subprocess.check_output
,正确地拆分参数,以便自动处理引用(切勿自己编写命令行)。 Here the -p
option must have the password without spaces (that's a command specificity), or use --password
which is clearer 在这里, -p
选项必须具有不带空格的密码(这是命令的特殊性),或者使用--password
更加清晰
output = subprocess.check_output([r'c:\xampp\mysql\bin\mysqldump', '-h', 'localhost', '-u', username, '--password', password, database])
Note: it seems that your password is blank (empty string), in that case, just use --password=
like explained here 注:看来你的密码为空(空字符串),在这种情况下,只要使用--password=
类似解释这里
output = subprocess.check_output([r'c:\xampp\mysql\bin\mysqldump', '-h', 'localhost', '-u', username, '--password=', database])
now the output of your command is in output
(if the command succeeds), you can write it into a file (well, in your case, it's true that you'be be better off with check_call
and stdout=file_handle
because you seem to need the .sql
file, not only the buffer) anyway: 现在命令的输出在output
(如果命令成功),您可以将其写入文件(当然,对于您而言,使用check_call
和stdout=file_handle
确实更好,因为您似乎需要.sql
文件,而不仅仅是缓冲区):
with open("file.sql","w") as f:
f.write(output)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.