1

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.

Mr Pickel
  • 95
  • 10
  • 2
    Do you have filenamed `record-0.csv` open while programming is running? – niraj Mar 14 '18 at 15:51
  • no, nothing is open except python shell and idle – Mr Pickel Mar 14 '18 at 15:53
  • @MrPickel check again. Does *your* code keep the file open in a loop and tries to *re*open it without closing? That `with open` inside the `if` looks very, very, very suspicious. So does the `f.close()` that runs *outside* the `if` block that created the file – Panagiotis Kanavos Mar 14 '18 at 15:54
  • @Panagiots Kanavos I did try to implement the `f.close()` inside the with open declaration (as you can see in updated code), but the error keeps coming. :-( – Mr Pickel Mar 15 '18 at 07:12
  • What's strange is: if I let the script write the files directly to desktop with `outfile = 'C:\\Users\\ebhproduser\\Desktop\\' + filename` and `filename = 'record-' + str(recordNr) + '.csv'` it works like a charm. But if I try to write in a subfolder I get the error. – Mr Pickel Mar 15 '18 at 08:24

0 Answers0