Python 脚本通过双击和 IDLE 运行,但不通过 Windows CMD shell 运行

发布于 2024-10-28 16:32:49 字数 689 浏览 1 评论 0原文

我遇到一个问题,如果我双击我的脚本 (.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2024-11-04 16:32:49

该脚本正在尝试写入当前目录中的文件。在上面的示例中,您从 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.

安穩 2024-11-04 16:32:49

这是因为当您双击该文件时(或从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文