如何制作新的.py文件

问题描述 投票:0回答:1

我想创建新文件,但首先选择目录。 此代码有效,但目录已设置

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')

也尝试过这样但没有发生

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')
python python-3.x directory
1个回答
0
投票

askdirectory 应该可以工作。

file_path = filedialog.askdirectory
© www.soinside.com 2019 - 2024. All rights reserved.