Python 脚本通过双击和 IDLE 运行,但不通过 Windows CMD shell 运行
我遇到一个问题,如果我双击我的脚本 (.py),或者使用 IDLE 打开它,它将正确编译并运行。但是,如果我尝试在 Windows 命令行中运行该脚本,使用
C:\> "C:\Software_Dev\Python 2.7.1\python.exe" C:\path\to\script\script.py
我得到...
Traceback (most recent call last):
File "C:\path\to\script\script.py", line 66, in <module>
a.CheckTorrent()
File "C:\path\to\script\script.py", line 33, in script
self.WriteLog(fileName)
File "C:\path\to\script\script.py", line 54, in WriteLog
myFile = open(r'%s' %(filename), 'w')
IOError: [Errno 13] Permission denied: './TorrentMonitor.log'
所以我的问题是,为什么当我在 Windows 7 中通过命令行运行此脚本时会出现权限错误,而当我双击时却不会?这两个过程有什么区别?
提前致谢!
I'm have a problem where if I double click my script (.py), or open it with IDLE, it will compile and run correct. However, if I try to run the script in my windows command line, using
C:\> "C:\Software_Dev\Python 2.7.1\python.exe" C:\path\to\script\script.py
I get...
Traceback (most recent call last):
File "C:\path\to\script\script.py", line 66, in <module>
a.CheckTorrent()
File "C:\path\to\script\script.py", line 33, in script
self.WriteLog(fileName)
File "C:\path\to\script\script.py", line 54, in WriteLog
myFile = open(r'%s' %(filename), 'w')
IOError: [Errno 13] Permission denied: './TorrentMonitor.log'
So my question is, why am I getting permission errors when I run this script through command line in window 7 but not when I double click? What's the difference between those two processes?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该脚本正在尝试写入当前目录中的文件。在上面的示例中,您从
C:\
启动它,您可能没有写入权限。cd
到您拥有的目录,您应该能够正常运行该命令。The script is trying to write into a file in the current directory. In the example above, you're starting it from
C:\
where you probably don't have write permissions.cd
to a directory that you own, and you should be able to run that command just fine.这是因为当您双击该文件时(或从 IDLE 运行该文件时),当前工作目录是包含脚本的目录。从命令行启动它时,您似乎没有写入权限的
C:\
。This is because when you double-click the file (or when running it from IDLE), the current working directory is the directory that contains your script. When starting it from the command line, it's
C:\
which you don't seem to have write access to.