0

I had some problems with my Python code. My code:

import os
logininfo = list()
with open(os.getcwd() + '/login/info.txt', 'r') as f:
     logininfo = f.readlines()

But my code isn’t work, so how do I fix that?

Edit: I changed the quote and changed the ‘ to '

Problem 2: After I fix all that look like my computer is freeze now and I can’t even move my mouse. After freeze for a while, the code make my computer ran to BSOD. What happened?

Okay, I think I see what the problem in problem 2 that my file was too big with 50 GB of login information of my server. Thanks you guy for helping me solve the problem 1.

  • 2
    what do you mean code isn't work, what is the resulting output, full text of Traceback error? That will likely show us all the source of the problem – chickity china chinese chicken Sep 27 '21 at 22:47
  • 2
    why are you using two // ? – Andressa Cabistani Sep 27 '21 at 22:47
  • can you add more about the error? – Andressa Cabistani Sep 27 '21 at 22:48
  • 1
    @Andressa Cabistani I believe thats the problem. – sur0k Sep 27 '21 at 22:48
  • Also the string for the `'r'` wasn't using single quotes, it was using this one: ` – Andressa Cabistani Sep 27 '21 at 22:52
  • if that `‘//login//info.txt’` is the same unicode characters in your code as posted, you should get `SyntaxError: invalid character in identifier` for the, `‘`, curly single quotes are unvalid unicode for python syntax. possibly replace those characters with `'`, `'//login//info.txt', 'r'` – chickity china chinese chicken Sep 27 '21 at 22:52
  • Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question and https://meta.stackoverflow.com/questions/359146/why-should-i-post-complete-errors-why-isnt-the-message-itself-enough and https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. "It doesn't work... please tell me" is not a question we can answer. It's not a question at all, in fact. – Karl Knechtel Sep 27 '21 at 22:57
  • ....full Traceback outputs are usually always helpful in these cases of "so anyone can see the problem,".... – chickity china chinese chicken Sep 27 '21 at 23:01

3 Answers3

2

Your problem is likely that your / (forward slashes) are supposed to be \ (backslashes). It is also a good practice to use os.path.join() when concatenating file paths. Make sure login\info.txt does not have a backslash in front of it. I printed the list afterwards to make sure it was working. Windows file paths use \\.

import os
with open(os.path.join(os.getcwd(), 'login\info.txt'), 'r') as f:
    logininfo = f.readlines()
print(logininfo)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
0

I believe the wrong thing is that you're using double slashs, when it should be:

import os

with open(os.getcwd() + ‘/login/info.txt’, 'r') as f:
     logininfo = f.readlines()

I reproduced the error here, created a file with the same folder structure as yours, and this definitely should work:

In [3]: with open(os.getcwd() + '/login/info.txt', 'r') as f:
   ...:     lines = f.readlines()
   ...:     for line in lines:
   ...:         print(line)
   ...: 
Olá,



deixa eu ver esse erro aqui
Andressa Cabistani
  • 463
  • 1
  • 5
  • 14
0

Regardless of OS, I would recommend using pathlib module to deal with system paths and to decrease ambiguity in OS path handling. So, regardless of OS, an API would be (including your code):

from pathlib import Path

file_path = Path.cwd() / 'login' / 'info.txt'

with open(file_path, 'r') as f:
    login_info = f.readlines()

Get familiar with that module (it's out of the box!) here: https://docs.python.org/3/library/pathlib.html

sur0k
  • 334
  • 1
  • 6
  • `file_path` here is an explanatory variable to increase readability. Remember, code reads from top to bottom, not from left to right. – sur0k Sep 27 '21 at 23:01