【问题标题】:Open a text file from python script in windows在 windows 中从 python 脚本打开一个文本文件
【发布时间】:2021-03-22 06:02:17
【问题描述】:

我正在用 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 中,我不知道如何从终端打开文本文件并将其显示给用户。一些建议?非常感谢

【问题讨论】:

  • notepad sample_data.txt?

标签: python windows cmd terminal text-editor


【解决方案1】:

这很简单。在 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')

【讨论】:

  • 我认为您找不到适用于 MacOS 的 gedit。 (如果你没有运行 Gnome,它可能也没有安装在 Linux 上。)
  • TextEdit 与 OSX 捆绑在一起
猜你喜欢
  • 2021-06-22
  • 2011-12-20
  • 2011-11-23
  • 2011-07-28
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 1970-01-01
相关资源
最近更新 更多