繁体   English   中英

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

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

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