[英]Creating a new .py file in python on a mac but getting [Errno 1] Operation not permitted
I have tried to write a python program that creates another python program and when I direct it to where I want it saved it says:我试图编写一个 python 程序来创建另一个 python 程序,当我将它定向到我想要保存的位置时,它说:
`File "/Users/Fredrik/Documents/infinitylock copy.py", line 11, in createFile
f = open(dest,'w')
IOError: [Errno 1] Operation not permitted: '/Users/Fredrik/Documents/python\\'`
My entire code is:我的整个代码是:
import time as t
from os import path
def createFile(dest):
date = t.localtime(t.time())
name = '%d_%d_%d.txt' % (date[1], date[2], (date[0] %100))
if not (path.isfile(dest + name)):
f = open(dest + name,'w')
f.write('\n'*30)
f.close()
if __name__ == '__main__':
destination = '/Users/Fredrik/Documents/python\\'
createFile(destination)
raw_input("done")
Please answer on how I can get this program to work properly.请回答我如何让这个程序正常工作。 And resolve the error.并解决错误。 Thank you.谢谢你。
Suggestions for 'get this program to work correctly': '让这个程序正常工作'的建议:
What I would do is go through each part of the code, and look up on StackOverflow on how most people use each part.我会做的是检查代码的每个部分,并在 StackOverflow 上查找大多数人如何使用每个部分。 For example, I would use例如,我会使用
import os
instead of代替
from os import path
simply because you might later want to use other functions.仅仅是因为您以后可能想要使用其他功能。 Its important to keep your code reusable, especially if it gets rid of errors later on.保持代码可重用很重要,尤其是在以后消除错误的情况下。
Answer to 'resolve the error':回答“解决错误”:
Try using forward slashes - I don't know why you have 'python\\' The program is likely throwing an error because you wrote that, an it is not valid and so therefore not permitted.尝试使用正斜杠 - 我不知道为什么你有 'python\\' 该程序可能会抛出一个错误,因为你写了它,它是无效的,因此不被允许。 Use .txt or some extension like that.使用 .txt 或类似的扩展名。 Finally, try to run it with administrative privileges.最后,尝试以管理权限运行它。 http://www.wikihow.com/Open-Applications-With-Root-Privileges-on-a-Mac has a fairly good explaination. http://www.wikihow.com/Open-Applications-With-Root-Privileges-on-a-Mac有一个很好的解释。 Simply open the console, root yourself, then run the program from there.只需打开控制台,root 自己,然后从那里运行程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.