繁体   English   中英

我有两个(.py)文件。 当第一个程序结束时,它将自行关闭,然后打开并运行第二个程序文件。 我该怎么做?

[英]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?

这是我的第一个文件的代码:

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 将 csv 转换为 xlsx我需要你帮助关闭这个文件并跳转到另一个文件(这是一个转换器程序,名为“converter.py”)。所以当我运行这个程序时,它会更新 csv 文件并跳转到“converter.py”以从转换后的 csv 更新 excel 文件。(删除旧的 xlsx 并创建从 csv 转换的新 xlsx)

最佳解决方案

我的建议是将其中一个 python 文件转换为一个包,然后简单地导入所需的函数。

因此,在这种情况下,把你的CSV转换,从conversion.py到像一个函数csv_to_xlsx()然后from conversion import csv_to_xlsx()然后在你的主文件的末尾运行。

其他解决方案

您可以使用subprocess与模块subprocess.Popen()使用os.system() 。 这些将允许您从另一个文件执行 python 代码。 同样最糟糕的其他解决方案是exec()这允许您将 python 代码作为字符串传递并执行它。 所以你可以这样做

exec(open('file.py', 'r').read())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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