Windows 10上的权限被拒绝[Errno13]

时间:2018-03-14 15:48:38

标签: python file-permissions

不幸的是我无法弄清楚为什么会出现这种错误。试图将os.chmod设置为0o777并将目标设为os.chdir(dir),但错误仍然存​​在。希望你能帮助我。

#!/usr/bin/env python 
'''This version of the readserial program demonstrates using python to write an output file'''
from datetime import datetime 
from datetime import date
import serial, io
import socket
import os
TCP_IP = 'localhost'
TCP_PORT = 23
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
recordNr = 0
firstLine = "Time\tMagnetometer Values [uT]\t\t\tAccelerometer Values[m/s2]"
secondLine = "\txAxis\tyAxis\tzAxis\txAxis\tyAxis\tzAxis"
#outfile='C:/Users/ebhproduser/Desktop/serial.csv'
ser = serial.Serial(port='COM5', baudrate=9600)
sio = io.TextIOWrapper(
    io.BufferedRWPair(ser, ser, 1),
    encoding='ascii', newline='\r' 
    )

while ser.isOpen(): 
        today = str(date.today())
        datastring = sio.readline()
        try:
            data = [abs(float(x)) for x in datastring.split(';')]
        except ValueError:
            continue

        dir = r'C:/Users/ebhproduser/Desktop/'+today + '/'
        filename = 'record-' + str(recordNr) +'.csv'
        outfile = dir+filename  
        if not os.path.exists(dir):
            os.makedirs(dir, os.umask(0o777))
            os.chdir(dir)
        #os.chmod(outfile,0o777)
        if (data[2] < 1000):
            with open(outfile,'a+') as f: #appends to existing file and '+' bedeutet, dass das File erstellt wird, falls nicht existent   
                MESSAGE = datetime.utcnow().isoformat()
                #\t is tab; \n is line separator
                f.write(datetime.utcnow().isoformat() + '\t' + datastring + '\n')
                f.flush() #included to force the system to write to disk
                s.send(MESSAGE.encode())
                f.close()
        else:
            f.close()
            recordNr = +1
ser.close()

错误是:

 Traceback (most recent call last):
  File "C:\Users\ebhproduser\Desktop\new1.py", line 38, in <module>
    with open(outfile,'a+') as f: #appends to existing file and '+' bedeutet, dass das File erstellt wird, falls nicht existent
PermissionError: [Errno 13] Permission denied: 'C:/Users/ebhproduser/Desktop/2018-03-14/record-0.csv'

更新:关于错误的事情必须是处理对创建的文件夹的写权限的事情。由于os.chmod不能(完全)在WINDOWS上运行。因此,如果有人遇到同一问题,我建议您阅读this onethis one

0 个答案:

没有答案