Unfortunately I can't figure out why this error occurs. Tried also to set os.chmod
to 0o777 and make dir to os.chdir(dir)
but the error still remains. Hope you can help me out.
#!/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()
Error is:
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'
UPDATE: The thing about the error must be something dealing with write permissions to created folder. As os.chmod
not (fully) works on WINDOWS. So if somebody facing the same issue I recommend to read this one and this one.