Python Forum
directory not being created after os.mkdir()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
directory not being created after os.mkdir()
#1
I'm creating an application that will parse text files into a different text file and create an excel spreadsheet from the new text file it has created. I have gotten everything working apart from being able to create the directory. I need to create the directory manually even though I am using os.mkdir. What am I doing wrong? I started learning python a few days ago so help would be appreciated. Here is my code:
import os
import tkinter as tk
from tkinter import ttk
import csv
import openpyxl
class dataFormatter():
    def SelectFile(self):
        drive = self.DriveField.get()
        availableDirs = os.listdir(drive + '/')
        self.DirField = ttk.Combobox(self.mainframe, values=availableDirs)
        self.DirField.grid(row=3, column=0)
        self.dirButton = ttk.Button(self.mainframe, text = 'Go', command = self.selectDataType)
        self.dirButton.grid(row= 3, column = 1)
        self.dirText = ttk.Label(self.mainframe, text='Select folder:', background='white', pad=10)
        self.dirText.grid(row=2, column=0)
    def selectDataType(self):
        driveDir = self.DirField.get()
        drive = self.DriveField.get()
        dataType = os.listdir(f'{drive}/{driveDir}')
        self.textTypeField = ttk.Label(self.mainframe, text = 'Select data type:', background = 'white', pad = 10)
        self.textTypeField.grid(row = 4, column = 0)
        self.TypeField = ttk.Combobox(self.mainframe, values = dataType)
        self.TypeField.grid(row = 5, column = 0)
        self.TypeFieldButton = ttk.Button(self.mainframe, text = 'Format', command = self.formatData)
        self.TypeFieldButton.grid(row = 5, column = 1)
    def formatData(self):
        selectedDataType = self.TypeField.get()
        print(selectedDataType)
        selectedDrive = self.DriveField.get()
        selectedDir = self.DirField.get()
        readFileFullPath = f'{selectedDrive}/{selectedDir}/{selectedDataType}'
        newFile = (f'Y:/ENGINEERING/{selectedDir}/{selectedDataType}')

        fileList = os.listdir(f'{readFileFullPath}/')
        for file in fileList:
            writeFilePath = f'{newFile}/Text'
            writeFileFullPath = f'{writeFilePath}/Parsed_{selectedDataType}_{file}'
            if os.path.exists(writeFilePath):
                os.chdir(writeFilePath)
            else:
                os.mkdir(writeFilePath)
            with open (f'{readFileFullPath}/{file}', 'r') as readFile, open(writeFileFullPath, 'w') as writeFile:
                readFile = readFile.readlines()
                errorCount = 0
                for line in readFile:
                    line = line.strip()
                    if line.find('ERROR') != -1:
                        errorCount += 1
                    else:
                        writeFile.write(f'{line}\n')
                writeFile.write(f'\nErrors: {errorCount}\n')
                writeFile.close()

            excelFile = file.replace('.txt', '.xlsx')
            xlsxFile = f'{newFile}/Excel/Parsed_{excelFile}'
            workbook = openpyxl.Workbook()
            worksheet = workbook.worksheets[0]
            with open(writeFileFullPath, 'r') as data:
                reader = csv.reader(data, delimiter = ' ')
                for row in reader:
                    worksheet.append(row)
                    workbook.save(xlsxFile)

        self.success_field = ttk.Label(self.mainframe, text = 'Formatting successful', background = 'white')
        self.success_field.grid(row = 6, column = 0, pady = 10)

    def __init__(self):
        drives = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'H', 'J', 'K', 'L', 'M',
                  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
        availableDrives = []
        for x in range(0, 1):
            for y in drives:
                if os.path.exists(y + ':/') == True:
                    if y + ':' not in drives:
                        availableDrives.append(y + ':')
        self.root = tk.Tk()
        self.root.geometry('350x250')
        self.root.title('Data Formatter')
        self.mainframe = tk.Frame(self.root, background = 'white')
        self.mainframe.pack(fill = 'both', expand = True)
        self.driveText = ttk.Label(self.mainframe, text = 'Select drive: ', background = 'white', pad = 10)
        self.driveText.grid(row=0, column=0, padx = 70)
        self.DriveField = ttk.Combobox(self.mainframe, values=availableDrives)
        driveButton = ttk.Button(self.mainframe, text='Go', command=self.SelectFile)
        driveButton.grid(row=1, column=1)
        self.DriveField.grid(row = 1, column = 0)
        self.root.mainloop()
        return

if __name__ == '__main__':
    dataFormatter()
and here is the error and terminal output:
Error:
File "C:\Users\christopher.donnelly\PycharmProjects\HelloWorld\pythonProject\readWriteDataLogging.py", line 42, in formatData os.mkdir(writeFilePath) FileNotFoundError: [WinError 3] The system cannot find the path specified: 'Y:/ENGINEERING/DataLogging/Ozone_Meter/Text' Ozone_Meter Process finished with exit code 0
Reply
#2
mkdir() cannot make Y: as Y: is a disk, not a directory. Does Y: already exist? If not, you'll need to mount it.

Given that you cannot create Y:, it makes no sense to have this:
        drives = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'H', 'J', 'K', 'L', 'M',
                  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
Instead of that you should check what drives are available. This is an easy way to get a list of available drives:
from pathlib import Path

def drives():
    for letter in 'ABCDEFGHIJKLMNOPQRSTUFWXYZ':
        path = Path(f'{letter}:')
        if path.exists():
            yield path

print(*list(drives()), sep="\n")
Reply
#3
I am not creating Y. Y is a network drive that is available on all computers in my company. The file I am trying to create or change is e.g.:
Y:/ENGINEERING/Data/Ozone_Meter

The ‘drives’ list is just the alphabet and the program checks to see what letters are available drives, which is then stored in the ‘available drives’ list.
Reply
#4
os.mkdir() will not create Y:/ENGINEERING/Data/Ozone_Meter if Y:/ENGINEERING/Data does not exist. Are you trying to make two or more directories at the same time?

Why are you using multiple comboboxes instead of the file dialog to pick a file?

https://docs.python.org/3/library/dialog...filedialog

Maybe you removed all the verical spacing to make the code more compact for posting, but the convention is to have one blank line between methods in a class, two blank lines surrounding classes and two blank lines surrounding functions

Another convention is to use pascal case for class names (DataFormatter instead of dataFormatter) and snake case for method and variable names (select_file instead of SelectFile, drive_text instead of driveText). I know Tk doesn't do it this way, but tkinter is copying C++ conventions because it is a wafer-thin Python wrapper around the Tk library (which is written in C).

When passing named arguments to a function or method, the convention is to not have whitespace around the equal sign (background=white instead of background = white). Some IDE's have tools that flag or automatically correct this kind of thing. I suggest you find one. It makes life a lot easier and do a lot toward teaching common coding conventions.

The __init__ method should be at the top of your class, not the bottom.

mainloop() should not appear inside a class UNLESS that class is designed to be modal.

I also think it is silly to make a window class that does not subclass a window. dataFormatter should be a root window instead of make a root window.
class DataFormatter(tk.Tk):
    def __init__(self, *args, **kwargs)
        super().__init__(*args, **kwargs)
        self.mainframe = tk.Frame(self, background='white')
Again, I know there are a lot of tkinter examples that make a class that makes the root like this (self.root = tk.Tk()) and then populates the root with widgets. Some of these are really old examples and others are regurgitated old examples. The only time you should explicitly call Tk() is when using a designer program that forces using this convention.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo exe created by pyinstaller only works in the dist directory dee 7 2,819 Feb-23-2023, 05:53 PM
Last Post: dee
  OSERROR When mkdir Oshadha 4 1,718 Jun-29-2022, 08:50 AM
Last Post: DeaD_EyE
  Python Paramiko mkdir command overwriting existing folder. How can i stop that? therenaydin 1 3,234 Aug-02-2020, 11:13 PM
Last Post: therenaydin
  mkdir() help SteampunkMaverick12 7 4,612 Feb-18-2018, 03:20 PM
Last Post: SteampunkMaverick12

Forum Jump:

User Panel Messages

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