Python 3.x jupyter笔记本在写入文件时缓冲,直到关闭?如何在不关闭jupyter笔记本的情况下查看结果?

Python 3.x jupyter笔记本在写入文件时缓冲,直到关闭?如何在不关闭jupyter笔记本的情况下查看结果?,python-3.x,file-io,jupyter-notebook,jupyter,Python 3.x,File Io,Jupyter Notebook,Jupyter,我是python编程新手,在高中时自学过quickbasic,现在正试图用我现在仅有的工具自学python——一本运行crouton的chromebook,在linux上安装了jupyter笔记本(这可能是我的第一组错误,用“有趣的”工具自学python)设置,但我在这里)。无论如何,我经常在我的手机上找到网络上有趣的链接,并通过电子邮件发送给我自己以供以后查看,我想我应该创建一个python程序来对URL列表进行排序并将它们输出到文件中 我把下面的代码包括进去了,这确实有效,但我遇到的问题是j

我是python编程新手,在高中时自学过quickbasic,现在正试图用我现在仅有的工具自学python——一本运行crouton的chromebook,在linux上安装了jupyter笔记本(这可能是我的第一组错误,用“有趣的”工具自学python)设置,但我在这里)。无论如何,我经常在我的手机上找到网络上有趣的链接,并通过电子邮件发送给我自己以供以后查看,我想我应该创建一个python程序来对URL列表进行排序并将它们输出到文件中

我把下面的代码包括进去了,这确实有效,但我遇到的问题是jupyter笔记本本身,我用谷歌搜索了一下,试图找到答案,但到目前为止没有成功。以下是实际问题:

当我在jupyter笔记本中写入文件时,它不会显示在本地文件系统中,直到我关闭jupyter笔记本。这不太理想,因为我喜欢使用jupyter笔记本进行快速测试和bug修复,而不是使用idle或bash,因此关闭jupyter笔记本只是为了查看输出到文件的内容是没有帮助的。有没有办法刷新缓冲区或其他东西,以便能够在文本编辑器中打开文本文件并查看结果,而不必退出jupyter笔记本

-如果需要,以下是程序:

##################################################################
#                                                                #
# parse_my_search.py - I save URLs to a text file for later      #
# viewing and/or bookmarking. These lists grow rather quickly,   #
# so this is a program to sort them by certain subject or        #
# websites and make sure that I have no duplicates.              #
#                                                                #
##################################################################


import os.path
from os import listdir


URL_set = {}   # set - used to remove duplicates when sorting/organizing
URL_list = []  # list - used to allow duplicates when sorting/organizing

temp_list = [] # list - temporary usage, discard or reassign to [] when not used

input_file = ""   # file to read in, get the URLs, and then close  

output_file = 'python.txt'  
                  # file name for output. Will probably change this to a list to write the 
                  # output files to. This should NOT overwrite
                  # the existing file, but it should open a python file (as in, a text 
                  # file with extention '.txt' which has the URLs that include the word
                  # 'python' in it), a youtube file (any/all youtube URLs), and an 'else' 
                  # file that is the catch-all for anything that is not in the first two
                  # files. NOTE: this has not been done yet, only opens single file


input_file = 'My_searches.txt'

while True:
    try:
        #for elem in os.listdir('.'):
        #    print(elem)

        #print(os.listdir('.'), end=" ")
        #print(onlyfiles)
        #print("enter filename")     
        #input_file = input()
        read_from = open(input_file)
        break
    except OSError as err:
            print("OS error: {0}".format(err))


ItemsReadInFromFile = read_from.readlines()

for item in ItemsReadInFromFile:
    URL_list.append(item.strip('\n'))

# using list comprehension to 
# perform removal of empty strings
URL_list = [i for i in URL_list if i] 

# removing duplicates:
URL_set = set(URL_list)
URL_list = list(URL_set)

URL_list.sort() # this will change the list/ is destructive. No need to do the following:
                #     URL_list = URL_list.sort()
                # In fact, doing so returns 'None'

# sorting for python        
write_to = open(output_file, 'w+')
for item in URL_list:
    if 'python' in item.lower():
        item += ('\n')
        write_to.write(item)



# this is a test that the program works to this point                    
print("so far so good", input_file)
read_from.close()

我今天找到了答案,结果证明它根本不是jupyter,而是因为在写入数据后,没有在程序结束时关闭文件。这就是为什么jupyter没有将数据写入文件并在文本编辑器中显示(直到jupyter关闭),文件仍然打开

在此处插入facepalm


一个新手犯的错误,有点尴尬,但是我很高兴我发现了。现在可以关闭了

我今天找到了答案,结果证明它根本不是jupyter,而是因为在写入数据后,没有在程序结束时关闭文件。这就是为什么jupyter没有将数据写入文件并在文本编辑器中显示(直到jupyter关闭),文件仍然打开

在此处插入facepalm

一个新手犯的错误,有点尴尬,但是我很高兴我发现了。现在可以关闭了