Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python脚本没有';t在读取大文件后终止_Python_Io - Fatal编程技术网

Python脚本没有';t在读取大文件后终止

Python脚本没有';t在读取大文件后终止,python,io,Python,Io,我编写了一个简短的Python脚本,用于在12GB文件中读取: start = time.time() my_file = open('my_12GB_file.txt', 'rb') my_file_lines = set(my_file.readlines()) end = time.time() print "Time elapsed: %r" % (end - start) my_file.close() 脚本读入文件,打印经过的时间,然后暂停(好像它进入了无限循环)。对可能

我编写了一个简短的Python脚本,用于在12GB文件中读取:

start = time.time()

my_file = open('my_12GB_file.txt', 'rb')

my_file_lines = set(my_file.readlines())

end = time.time()

print "Time elapsed: %r" % (end - start)

my_file.close()
脚本读入文件,打印经过的时间,然后暂停(好像它进入了无限循环)。对可能出现的问题有什么想法吗

更新:

程序在我更改后终止:

my_file_lines = set(my_file.readlines())


在读取文件时,非常建议将python内置的
一起使用,尤其是对于大型文件:

with open("my_12GB_file.txt") as large_file:
    for line in large_file:
        do_something(line)
负责在完成或出现故障时关闭文件。
如果您逐行读取文件,它也不会将整个文件加载到内存中,这可能是您遇到的问题。

似乎没有足够的内存可用,因此程序需要很长时间,有两种解决方案,一种是您可以将文件分解为小文件,另一种是您可以尝试使用超过12GB的ram。在我看来,第一种选择更可行

什么是摊位?它没有终止吗?它没有终止吗?您有32位python吗?如果将
my_file.close()
移动到
end=time.time()
之前,会发生什么情况?经过的时间被打印出来了吗?我想你应该有耐心。即使最后你可能会得到一份工作。参见,例如,克服这种情况。
with open("my_12GB_file.txt") as large_file:
    for line in large_file:
        do_something(line)