[Python] How to make a new .py file

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I want to make new file but first choose directory. This code works but a directory is already set

    new_file = input("File name\n")
    new_file = new_file.lower().replace(".", "").replace(" ", "_")
    print(new_file)
    open_file = open('D:\Python projects\%s.py' % new_file, 'w')


    Also tried like this but not happen

    import tkinter as tk
    from tkinter import filedialog

    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename()
    new_file = input("Name file\n")
    open_file = open(f"{file_path}\%s.py" % new_file, 'w')

    Continue reading...

Compartilhe esta Página