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.

8
  • 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 Commented Sep 27, 2021 at 22:47
  • 2
    why are you using two // ? Commented Sep 27, 2021 at 22:47
  • can you add more about the error? Commented Sep 27, 2021 at 22:48
  • 1
    @Andressa Cabistani I believe thats the problem. Commented Sep 27, 2021 at 22:48
  • Also the string for the 'r' wasn't using single quotes, it was using this one: ` Commented Sep 27, 2021 at 22:52

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

file_path here is an explanatory variable to increase readability. Remember, code reads from top to bottom, not from left to right.
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

4 Comments

I fix that but it still didn’t work!
did you fix the 'r' too??
Yeah I fix it just see my problem 2 bro!
try the changes I added in the edition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.