簡體   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