[英]Open a text file from python script in windows
我正在 Python 中創建一個需要在 Unix 和 Windows 中運行的 GUI。 在 python 主腳本中,我需要創建並打開(如彈出窗口)一個文本文件。 我有 unix 操作系統的代碼,它是:
os.chdir(path=app.sourceFolder+"/Fastq_downloaded")
files = os.listdir(os.getcwd())
sample_data = open("sample_data.txt", "w")
sample_data.write("ID" + "\t" + "Condition" + "\n")
for i in range(len(files)-1):
sample_data.write(files[i][:-6] + "\t" + "\n")
sample_data.write(files[-1][:-6] + "\t")
sample_data.close()
if "Linux" in o_sys or "Darwin" in o_sys:
os.system('gedit sample_data.txt')
首先,我創建一個包含信息的文件,然后從終端調用 gedit 打開它。 但是在 windows 中,我不知道如何從終端打開文本文件並將其顯示給用戶。 一些建議? 非常感謝
這很簡單。 在 windows 中,您使用記事本。
代碼:
import sys
import os
if sys.platform == 'win32':
os.system('notepad sample_data.txt')
或添加到您的代碼中
if "Linux" in o_sys or "Darwin" in o_sys:
os.system('gedit sample_data.txt')
elif 'win32' in o_sys:
os.system('notepad sample_data.txt')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.