[英]I have got two (.py) files. When the first program comes to end, it will close itself then open and run the second program file. How can ı do it?
Here is my first file's codes:这是我的第一个文件的代码:
from csv import writer
film=[""] #The list contains informations about the film
#İnputs:
filmname=input("Film name: ")
category=input("category: ")
score=input("point: ")
score=str(score)
complement=input("complement: ")
valuablenames=input("İmportant actors: ")
#Appending Inputs
film.append(filmname)
film.append(category)
film.append(score)
film.append(complement)
film.append(valuablenames)
#The Function which appends the list into csv file:
def liste_append(filename,inf_list):
with open("FilmSet.csv", 'a+', newline='') as write_obj: # Open file in append mode
csv_writer = writer(write_obj) # Create a writer object from csv module
csv_writer.writerow(film) # Add contents of list as last row in the csv file
#Calling Function
liste_append("FilmSet.csv",film)
#Jumping to File which converts csv to xlsx
#Jumping to File which converts csv to xlsx The part i need your help to close this file and jump to the other one (which is a converter program and named as "converter.py").So when i run this program,it will update the csv file and jump to "converter.py" for updating excel file from csv converted.( deletes the old xlsx and creates new xlsx which is converted from csv ) #Jumping to File 将 csv 转换为 xlsx我需要你帮助关闭这个文件并跳转到另一个文件(这是一个转换器程序,名为“converter.py”)。所以当我运行这个程序时,它会更新 csv 文件并跳转到“converter.py”以从转换后的 csv 更新 excel 文件。(删除旧的 xlsx 并创建从 csv 转换的新 xlsx)
My recommendation would be to turn one of the python files into a package and simply import the required functions.我的建议是将其中一个 python 文件转换为一个包,然后简单地导入所需的函数。
So in this case put your csv conversion from conversion.py
into a function like csv_to_xlsx()
and then from conversion import csv_to_xlsx()
and then run it at the end of your main file.因此,在这种情况下,把你的CSV转换,从
conversion.py
到像一个函数csv_to_xlsx()
然后from conversion import csv_to_xlsx()
然后在你的主文件的末尾运行。
You could use the subprocess
module with subprocess.Popen() or os.system() .您可以使用
subprocess
与模块subprocess.Popen()或使用os.system() 。 These will allow you to execute python code from another file.这些将允许您从另一个文件执行 python 代码。 Also the worst other solution is
exec()
this allows you to pass python code as a string and execute it.同样最糟糕的其他解决方案是
exec()
这允许您将 python 代码作为字符串传递并执行它。 so you could do所以你可以这样做
exec(open('file.py', 'r').read())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.