用python编辑文本文件

时间:2014-12-12 21:58:26

标签: python

我正在尝试在python中编写一个实用程序,它将列出当前目录中的文本文件,让用户选择一个文件,然后在文本编辑器中打开该文件。如何在os.system命令中引用数组?

import os
from os import listdir
from os.path import isfile, join
mypath = os.getcwd()
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]

cnt = 0
for s in onlyfiles:
    cnt += 1
    print "%d : %s" % (cnt,s)
choice = raw_input("Select file: ")
os.system("kate onlyfiles[choice]")

2 个答案:

答案 0 :(得分:0)

os.system('kate "{}"'.format(onlyfiles[int(choice) - 1]))

更好的解决方案是:

subprocess.call(['vim', '{}'.format(onlyfiles[int(choice) - 1])])

答案 1 :(得分:-1)

感谢您的帮助。我将研究子流程

os.system("kate %s " % onlyfiles[int(choice)])
相关问题