Home > Software design >  I Have 3 Variables NAME, DIRECTORY, FILE_EXTENSION how to make a file with this information?
I Have 3 Variables NAME, DIRECTORY, FILE_EXTENSION how to make a file with this information?

Time:10-31

def createfile(name, location, extension):
    print(name, extension, location)

I have this code and I need to make a file with these variables

I have tried a couple of things but they never seemed to work.

CodePudding user response:

Adding code below for better understanding with output screenshot.

import os
def createfile(name, location, extension):
    print(name, extension, location)
    #starting creating a file with some dummy contents
    path = os.path.join(location, name   '.'   extension)
    f = open(path, "a")
    f.write("Your contents!! or whatever you want to put inside this file.")
    f.close()
    print("File creation is successful!!")

def readfile(name, location, extension):
    #open and read the file after the appending:
    path = os.path.join(location, name   '.'   extension)
    f = open(path, "r")
    print(f.read())

#pass the parameters here
createfile('test','./','txt')
readfile('test','./','txt')

enter image description here

CodePudding user response:

Here is a code using tkinter to get the name of the folder by an Entry widget. The code focuses on pdf files, but can be substituted for any file type.

This function is called fisrt with the path of your file or your locations, passing the path as 'files' *arg.

def replace_slash(files):

    ficheros=[]

    for i in files:

        pth=pathlib.Path(i)
        pth=str(pth)
        ficheros.append(pth.replace('/', '\\'))

    return ficheros


def save_file_window(file_to_save, extension):
    
        work_file = os.path.dirname(__file__) # Save the path of the working folder (where the '.py' file is located).
        all_files = os.listdir(work_file) # Saves all the files in the folder, in a list.
        location = ''.join(replace_slash(work_file)) # Where the file will be saved. You can replace it for an specific location.

        aux_root= Tk()
        aux_root.resizable(0,0)
        aux_root.title('Save file')
    
        aux_mf= Frame(aux_root)
        aux_mf.pack()
    
        def save_file():
            extension = ext_entry.get()

            if ent.get()== '':
                nom = 'file_doe'
            else:
                nom= ent.get()
    
    
            if extension in ent.get():
                nom= ent.get().replace(f'.{extension}', '')   
    
    
            if nom   f'.{extension}' in all_files:
                nom= nom   '-new'
    
    
            with open(f'{location}\\{nom}.{extension}', 'wb') as new_file:
                file_to_save.write(new_file)
            
            messagebox.showinfo(title= 'Saved', message= f'File saved successfully.'
                f'\n\nFile name: {nom}.{extension}'
                f'\n\nFile path: {destination}')
    
            aux_root.destroy()
    
        entVar= StringVar()
    
        lb= Label(aux_mf, text= 'Select the file name and the type below:', font= 'bold', pady= 15, padx= 30)
        lb.grid(row= 0, column= 0)
    
        ent= Entry(aux_mf, textvariable= entVar)
        ent.grid(row= 1, column= 0, pady= 15)

        ext_entry= Entry(aux_mf, textvariable= entVar)
        ext_entry.grid(row= 2, column= 0, pady= 15)    

        btn= Button(aux_mf, text= 'Send', pady= 5, font= 'bold', activebackground= "#38EB5C", cursor= 'hand2', bd= 5,width= 10,command= save_file)
        btn.grid(row= 3, column= 0)
    
        aux_root.mainloop()
  • Related