File is not being created with 'w' argument - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: File is not being created with 'w' argument (/thread-41750.html) |
File is not being created with 'w' argument - CAD79 - Mar-13-2024 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: 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.
RE: File is not being created with 'w' argument - paul18fr - Mar-13-2024 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}") RE: File is not being created with 'w' argument - deanhystad - Mar-13-2024 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. RE: File is not being created with 'w' argument - snippsat - Mar-14-2024 (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")
|