Permission denied error while writing to a file in Python

后端 未结 9 1875
萌比男神i
萌比男神i 2020-12-17 08:48

I want to create a file and write some integer data to it in python. For example, I have a variable abc = 3 and I am trying to write it to a file (which doesn\'t exist and I

相关标签:
9条回答
  • 2020-12-17 09:12

    To answer your first question: yes, if the file is not there Python will create it.

    Secondly, the user (yourself) running the python script doesn't have write privileges to create a file in the directory.

    0 讨论(0)
  • 2020-12-17 09:14

    Please close the file if its still open on your computer, then try running the python code. I hope it works

    0 讨论(0)
  • 2020-12-17 09:14

    I write python script with IDLE3.8(python 3.8.0) I have solved this question: if the path is shelve.open('C:\\database.dat') it will be PermissionError: [Errno 13] Permission denied: 'C:\\database.dat.dat'. But when I test to set the path as shelve.open('E:\\database.dat') That is OK!!! Then I test all the drive(such as C,D,F...) on my computer,Only when the Path set in Disk

    C:\\

    will get the permission denied error. So I think this is a protect path in windows to avoid python script to change or read files in system Disk(Disk C)

    0 讨论(0)
  • 2020-12-17 09:16

    This also happens when you attempt to create a file with the same name as a directory:

    import os
    
    conflict = 'conflict'
    
    # Create a directory with a given name
    try: 
        os.makedirs(conflict)
    except OSError:
        if not os.path.isdir(conflict):
            raise
    
    # Attempt to create a file with the same name
    file = open(conflict, 'w+')
    

    Result:

    IOError: [Errno 13] Permission denied: 'conflict'
    
    0 讨论(0)
  • 2020-12-17 09:18

    Make sure that you have write permissions for that directory you were trying to create file by properties

    0 讨论(0)
  • 2020-12-17 09:19

    Permission denied simply means the system is not having permission to write the file to that folder. Give permissions to the folder using "sudo chmod 777 " from terminal and try to run it. It worked for me.

    0 讨论(0)
提交回复
热议问题