[英]Error while trying to rename a directory through cmd command from python
I have this piece of python/tkinter code that is trying to rename a directory. 我有这段试图重命名目录的python / tkinter代码。 When the call() gets executed it trows an error.
当执行call()时,将引发错误。
if os.path.exists(self.destDirectory):
self.now = datetime.datetime.now()
print(self.now)
self.now = str(self.now.strftime("%Y_%m_%d_%H_%M"))
print('rename {0} {1}'.format(self.destDirectory, 'old_' + self.now))
self.cmd1 = ('rename {0} {1}'.format(self.destDirectory, 'old_' + self.now))
self.returnCode1 = call(self.cmd1)
ERROR: 错误:
Exception in Tkinter callback Traceback (most recent call last):
File "C:\Python32\lib\tkinter\__init__.py", line 1399, in __call__
return self.func(*args)
File "C:\EclipseWorkspaces\csse120\Myproject\src\BoxRestore.py", line 95, in proceed
self.returnCode1 = call(self.cmd1)
File "C:\Python32\lib\subprocess.py", line 467, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python32\lib\subprocess.py", line 741, in __init__
restore_signals, start_new_session)
File "C:\Python32\lib\subprocess.py", line 960, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
But When I do: 但是当我这样做时:
print('rename {0} {1}'.format(self.destDirectory, 'old_' + self.now))
And execute it in cmd I get no errors. 并在cmd中执行它,我没有任何错误。
Another command Won't complain: 另一个命令不会抱怨:
self.cmd2 = ('xcopy {0} {1} /I /E'.format(self.values['sourceButton'], self.values['destinationButton']))
self.returnCode = call(self.cmd2)
Can You Please Help. 您能帮忙吗?
You should use this: 您应该使用此:
if os.path.exists(self.destDirectory):
self.now = datetime.datetime.now()
print(self.now)
self.now = str(self.now.strftime("%Y_%m_%d_%H_%M"))
print('rename {0} {1}'.format(self.destDirectory, 'old_' + self.now))
os.rename( self.destDirectory, 'old_' + self.now )
There's no "rename" program on Windows. Windows上没有“重命名”程序。
Instead, "rename" is a built-in command in the command prompt program ( cmd.exe
). 而是,“重命名”是命令提示符程序(
cmd.exe
)中的内置命令。 So when you type "rename" at a command prompt, it gets treated specially by cmd.exe
. 因此,当您在命令提示符下键入“重命名”时,
cmd.exe
将对其进行特殊处理。
When you run a program using Python's subprocess
module, then (by default) it doesn't use cmd.exe
, it tries to run an actual program. 当您使用Python的
subprocess
模块运行程序时,(默认情况下)它不使用cmd.exe
,而是尝试运行实际程序。 This doesn't work for rename
. 这不适用于
rename
。 You can change this by passing the shell=True
option to subprocess.call
; 您可以通过将
shell=True
选项传递给subprocess.call
来更改此设置; if you do that then it should work. 如果您这样做,那么它应该可以工作。 (This also introduces a security vulnerability if you're getting any part of the command line from the Internet or something else you can't trust, which is why it's not the default).
(如果您是从Internet获取命令行的任何部分或其他您不信任的内容,这还会引入一个安全漏洞,这就是为什么它不是默认值)。
But using os.rename()
is a MUCH better solution - you get better error handling, your program will be faster, more reliable, more secure, simpler, easier for other programmers to understand, and portable to Linux/Mac. 但使用
os.rename()
是一个更好的解决方案-你得到更好的错误处理,你的程序将更快,更可靠,更安全,更简单,更容易为其他程序员理解,并移植到Linux的/ Mac的。 (One example of "more reliable": your current code breaks if the directory name contains a space; but os.rename()
will just work). (“更可靠”的一个示例:如果目录名包含空格,则当前代码会中断;但是
os.rename()
会起作用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.