简体   繁体   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?

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)

Best solution最佳解决方案

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()然后在你的主文件的末尾运行。

Other solutions其他解决方案

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.

相关问题 如何让我的 python 程序 (C-Bos) 只打开结尾带有“.bos.py”的文件? - How do I make my python program (C-Bos) open only files with ".bos.py" at the end? 为什么我在运行程序时输入的前两个输入会在文本文件中重复? - Why do the first two inputs I enter when I run my program repeat in the text file? 当我运行它的 .py 文件时,无法在 python 中写入文件。 但是当我在pycharm中运行它时,它确实有效 - Can't write files in python when I run it's .py file. But when I run it in pycharm, it actually work 我用什么打开 .PY 程序? - What do I open a .PY program with? 在QTable中打开CSV文件时程序关闭 - program close when open csv file in qtable 当我运行pyaudio程序时出现此错误,我该如何解决 - when I run the pyaudio program I got this error, how can l resolve this 如何在没有.py文件位于源位置的情况下使程序移动文件 - How do I make my program move files without the .py file being in the source location 当我没有文件标识符时,如何在python中关闭文件 - How do I close files in python when I haven't got the file identifiers 如何在没有 .py 扩展名的情况下运行 argparse python 程序? - How can I run an argparse python program without the .py extension? 手动关闭程序时可以让python做些什么吗? - Can I let python do something when manually close the program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM