简体   繁体   English

在 Mac 上的 python 中创建一个新的 .py 文件但得到 [Errno 1] 不允许操作

[英]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.

相关问题 在 mac 中安装 opencv-python 时,OSError: [Errno 1] Operation not allowed - When install opencv-python in mac, OSError: [Errno 1] Operation not permitted OSError:[Errno 1]在Mac上安装python quandl时不允许进行操作 - OSError: [Errno 1] Operation not permitted when installing python quandl on mac Python 中的 Chmod - [Errno 1] 不允许操作 - Chmod in Python - [Errno 1] Operation not permitted Python fcntl.ioctl [Errno 1] 不允许操作 - Python fcntl.ioctl [Errno 1] Operation not permitted Mac:OSError:[Errno 1]不允许操作:'/ tmp / pip-XcfgD6 - Mac: OSError: [Errno 1] Operation not permitted: '/tmp/pip-XcfgD6 在Mac上升级pip时出错:IOError:[Errno 1]不允许操作:'/ bin / pip' - Error upgrading pip on Mac: IOError: [Errno 1] Operation not permitted: '/bin/pip' 在macOS上安装virtualenvwrapper错误:[Errno 1]不允许的操作: - Install virtualenvwrapper on mac- OSError: [Errno 1] Operation not permitted: 使用 Python ftplib 上传文件时出现“不允许操作”或“不是常规文件” - Getting “Operation not permitted” or “Not a regular file” when uploading file with Python ftplib OSError: [Errno 1] 不允许操作: - OSError: [Errno 1] Operation not permitted: OSError:[Errno 1]不允许操作 - OSError: [Errno 1] Operation not permitted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM