[英]Python: Writing variables to a file in a specific way
所以我希望用戶選擇一個文件和一個文件夾。 該文件將被移動到所述文件夾,現在我想將其變量(x和y)及其值(路徑)寫入到另一個文件中,其中的shutil.move
模塊如下所示。 但是,我不知道如何在shutil.move
獲取零件x和y作為變量,從而將路徑而不是字母寫入file.py。 f.write
可以執行此操作,還是應該使用其他方法?
from tkinter import * from tkinter.filedialog import askopenfilename, askdirectory from tkinter.messagebox import showerror import shutil X = askopenfilename(filetypes=(("All files", "*.*"), ("HTML files", "*.html;*.htm"), ("Template files", "*.tplate"))) Y = askdirectory() f = open("OperatorTest.py", "w") f.write('shutil.move(X, Y)' )
您可以使用:
f.write('shutil.move({0}, {1})'.format(X, Y))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.