A
A
Akshin Yolchuev2020-05-09 13:56:56
Python
Akshin Yolchuev, 2020-05-09 13:56:56

Throws MemoryError?

with open(r'C:\Users\AKSHIN\Downloads\errr.txt','r',encoding='utf-8')as f:
  	z = f.read().split('\n')
  	for line in z:
              print(line)

there are 6 million lines in the text editor, because of this, the error
Traceback (most recent call last) is displayed:
File "C:\Users\AKSHIN\Desktop\python\chtoto.py", line 2, in
z = f.read().split( '\n')
MemoryError
[Finished in 0.6s with exit code 1]
[shell_cmd: py -3 -OO -u "C:\Users\AKSHIN\Desktop\python\chtoto.py"]
[dir: C:\Users \AKSHIN\Desktop\python]
[path: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel \Intel(R) Management Engine Components\iCLS\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\ windows\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common ;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\WINDOWS\system32;C:\WINDOWS ;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Users\AKSHIN\PycharmProjects\akshin\regchecker\insta\geckodriver.exe ;C:\Users\AKSHIN\AppData\Local\Programs\Python\Python38-32;]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2020-05-09
@Akshin17

with open(file_name, 'r') as f:
    for line in f:
        # your code

In this case, the file will be read line by line, rather than loaded completely into memory. This is called a generator , read more here .

S
Sergey Karbivnichy, 2020-05-09
@hottabxp

MemoryError - an exception that occurs in non-critical cases of exhaustion of free memory. You can handle it by removing objects from memory.
In general, you are doing bullshit. Output 6 million lines to the console. Your life is not enough to read every line. Output only those lines that contain the desired text (you can add more problems for yourself, use regular expressions - this is from a bearded joke)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question