如何将字典值中列出的文件转换为链接并使用assoc软件对该文件打开?

时间:2018-12-06 03:07:47

标签: python python-3.x

我的“文件”字典的值中列出了文件名。我如何编码,当字典返回这些值(文件)时,它们以某种超链接的形式返回,当您单击链接时,操作系统将自动打开与该文件关联的软件?

我知道这在终端中不起作用,但是现在我正在使用Jupyter Notebook。另外,代码将与文件位于同一目录中-谢谢。

def concept(phrase):

# split var(phrase) at spaces and assign to var(words)
words = phrase.split()

# use this to list python file titles and links to open them in a new tab
files    = {1:"file0001.txt",
            2:"file0002.txt",
            3:"file0003.txt",
            4:"file0004.txt",
            5:"file0005.txt",
            6:"file0006.txt",
            7:"file0007.txt",    
            8:"file0008.txt",
            9:"file0009.txt"}

# change keys to searchable simple keyword phrases. 
concepts = {'GAMES':[1,2,4,3,3],
            'BLACKJACK':[5,3,5,3,5],
            'MACHINE':[4,9,9,9,4],
            'DATABASE':[5,3,3,3,5],
            'LEARNING':[4,9,4,9,4]}

# iterate through all var(words) found in var(word)
for word in words:
# convert to uppercase, search var(word) in dict 'concepts', if not found return not found"
    if word.upper() not in concepts:
        print("'{}':Not Found in Database" .format(word)) not in concepts
    else:
# for matching keys in dict 'concept' list values in dict 'files'
        for pattern in concepts[word.upper()]:
            print(files[pattern])

# return input box at end of query        
while True:
    concept(input("Enter Concept Idea: "))

1 个答案:

答案 0 :(得分:0)

我认为即使在Jupyter笔记本电脑中,您也无法在控制台中创建自定义链接(除了Web链接,它们会自动转换为超链接)。但是,您可以使用以下命令在python中打开文件:

import subprocess

subprocess.call("start " + filename, shell=True) # On windows
subprocess.call("open " + filename, shell=True) # On Mac

希望有帮助。