Python Forum
File is not being created with 'w' argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File is not being created with 'w' argument
#1
I am trying to create a small program to parse a text file that outputs data from a datalogger, but from what I have looked up online, using 'w' as an argument in the open parameter should be enough. I only started coding in python yesterday so a bit of help would be appreciated. This is my code:

 import datetime


now = datetime.datetime.now()
readFileName = now.strftime("%d-%m-%y.txt")
dirName = "dataLogging"
readFileFullPath = ('E:/' + dirName + '/' + readFileName)
readFile = open(readFileFullPath, 'r')
writeFileName = 'parsed_' + readFileName
writeFileFullPath = ('C:/' + 'parsed_' + dirName + '/' + writeFileName)
writeFile = open(writeFileFullPath, 'w')
lines = readFile.readlines()
errorCount = 0
for line in lines:
    line = line.strip()
    if line.find("ERROR") != -1:
        errorCount +=1
    else:
        writeFile
        file.write(line)
        file.close()


writeFile("Errors:", errorCount) 
and this is the traceback I get:

Error:
Traceback (most recent call last): File "C:\Users\christopher.donnelly\PycharmProjects\HelloWorld\pythonProject\readWriteDataLogging.py", line 11, in <module> writeFile = open(writeFileFullPath, 'w') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'C:/parsed_dataLogging/parsed_13-03-24.txt' Process finished with exit code 1
Edit: never mind, I solved it, it was just the directory and a bit of syntax. The directory will already be created so all it needs to create is the text file.
Reply
#2
With the text file, I would have test something like :

import datetime
 
 
now = datetime.datetime.now()
readFileName = now.strftime("%d-%m-%y.txt")

dirName = "dataLogging"

readFileFullPath = ('E:/' + dirName + '/' + readFileName)

writeFileName = 'parsed_' + readFileName
writeFileFullPath = ('C:/' + 'parsed_' + dirName + '/' + writeFileName)


with open(readFileFullPath, 'r') as readFile, open(writeFileFullPath, 'w') as writeFile:

    lines = readFile.readlines()
    errorCount = 0
    
    for line in lines:
        line = line.strip()
        if line.find("ERROR") != -1:  
        # if "ERROR" in line.upper():   # another way
            errorCount +=1
        else:
            writeFile.write(line)
     
    writeFile.write(f"Errors:{errorCount}") 
Reply
#3
Opening a file using "w" does not create a folder, the folder must already exist. Only the file gets created automatically. If C:\parsed_datalogging exists and you still can't create files in the folder, check the folder permissions.
Reply
#4
(Mar-13-2024, 08:19 AM)CAD79 Wrote: dit: never mind, I solved it, it was just the directory and a bit of syntax. The directory will already be created so all it needs to create is the text file.
Some advice tips,look into pathlib
Also try to use with open and f-string make string nicer.
Then avoid line like this with added +🔨.
writeFileFullPath = ('C:/' + 'parsed_' + dirName + '/' + writeFileName)
Example.
from pathlib import Path

parsed_dir = Path('C:/dataLogging')
# Check if directory exists,if not make directory
parsed_dir.mkdir(parents=True, exist_ok=True)
file_path = Path('data.log')
with open(file_path) as in_file, open(parsed_dir / 'error_log.txt', 'w') as out_file:
    error_count = 0
    for line in in_file:
        line = line.strip()
        if "ERROR" in line:
            error_count += 1
        else:
            out_file.write(f"Lines with no <ERROR>: {line}\n")
    out_file.write(f"-------\nErrors: {error_count}\n")
Output:
Lines with no <ERROR>: 123 hello Lines with no <ERROR>: go home ------- Errors: 2
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on open of file created with tempfile.TemporaryDirectory() Brian177 4 6,307 Apr-05-2021, 07:12 PM
Last Post: Brian177
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,870 Dec-06-2020, 04:14 PM
Last Post: Divya577
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,851 Mar-03-2020, 08:34 AM
Last Post: buran
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,322 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Incorrect code output (File not being created) Hass 4 2,934 Jan-01-2019, 04:10 AM
Last Post: ichabod801
  Opening txt file created in python in excel fabipc 1 2,556 Aug-05-2018, 11:59 PM
Last Post: ichabod801
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,492 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb
  Accept Multiple Lines of Input into a User Created File Bragger 0 2,629 Oct-29-2017, 04:15 PM
Last Post: Bragger
  Calling a C Func though Pyhon Script using DLL file which is created for C file. CMMouli 4 4,672 Feb-18-2017, 05:06 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020