[英]Python Script Won't Write to text file when run from OS X terminal
So I've written a pretty basic script as part of a larger test of my system. 因此,在系统的大型测试中,我写了一个非常基本的脚本。 Essentially, it just creates a new file text.txt and writes "abject failure" to the file.
本质上,它只是创建一个新文件text.txt并将“失败失败”写入文件。
#!/usr/bin/env python
print "running"
text_file = open("text.txt", "a")
text_file.write("abject failure")
text_file.close()
print "success"
The script works wonderfully when I run from IDLE, but when I run from the Mac OS X terminal with 当我从IDLE运行时,脚本运行得非常好,但是当我从Mac OS X终端运行时,
python /Users/.../serverside.py
I only get the "print" values but nothing is written to the text.txt file. 我仅获得“打印”值,但未将任何内容写入text.txt文件。 Why is this and how can I fix it?
为什么会这样,我该如何解决?
To make sure that your script always works, no matter where your call is coming from, also set an absolute path to your file 为确保脚本始终有效,无论调用来自何处,都要设置文件的绝对路径
text_file = open("/Users/path/to/file/text.txt", "a")
The other solution, as stated in the comment, is to change your working directory to the path where you want the file to be 如评论中所述,另一种解决方案是将工作目录更改为您希望文件所在的路径。
cd /Users/path/to/file
python /Users/.../serverside.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.