简体   繁体   English

Python:以特定方式将变量写入文件

[英]Python: Writing variables to a file in a specific way

So i want the user to select a file and a folder. 所以我希望用户选择一个文件和一个文件夹。 The file will be moved to said folder, now i want to write those variables (x and y) with their value (the path) into another file.py with the shutil.move module in it as shown below. 该文件将被移动到所述文件夹,现在我想将其变量(x和y)及其值(路径)写入到另一个文件中,其中的shutil.move模块如下所示。 I am however lost at how I can get the part within shutil.move , the variables x and y, to act as variables and thus write the path instead of the letter into the file.py. 但是,我不知道如何在shutil.move获取零件x和y作为变量,从而将路径而不是字母写入file.py。 Is there a way within f.write to do this or should I use something else? 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM