简体   繁体   English

从 windows 中的 python 脚本打开一个文本文件

[英]Open a text file from python script in windows

I am creating a GUI in Python that needs to be ran in Unix and Windows.我正在 Python 中创建一个需要在 Unix 和 Windows 中运行的 GUI。 In the python main script I need to create and open (like a popup) a text file.在 python 主脚本中,我需要创建并打开(如弹出窗口)一个文本文件。 I have the code for unix operative system and it is:我有 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')

First I create a file with information and then I open it calling the gedit from the terminal.首先,我创建一个包含信息的文件,然后从终端调用 gedit 打开它。 But in windows I don't have any idea of how we can open a text file from terminal and show it to the user.但是在 windows 中,我不知道如何从终端打开文本文件并将其显示给用户。 Some advise?一些建议? Thank you very much非常感谢

This is simple.这很简单。 In windows you use notepad.在 windows 中,您使用记事本。

Code:代码:

import sys
import os

if sys.platform == 'win32':
    os.system('notepad sample_data.txt')

OR to add to your code或添加到您的代码中

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.

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