Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Importing User Pass file into Python

Hey guys! Quick question

Code
userpassFile = open("userpass.txt", "r")
list1 = userpassFile.readlines()
print(list1)

Gives: (type = list)

['username1:password1\n', 'username2:password2\n', 'username3:password3\n']

Now how would I go about

Splitting this into seperate values so i can work with the user and pass independently?

Thank you

Edit: Here's what I ended up with, works, but it's messy.

Code
userpassFile = open("userpass.txt", "r")
list1 = userpassFile.readlines()
print(list1)

for each in list1:
    each = each.strip()
    # print(each.split(':')[0])
    usernameStr = (each.split(':')[0])
    passwordStr = (each.split(':')[1])
    print(usernameStr)
    print(passwordStr)
thank for posting your solution. split is not so messy. good job
[Image: 3GxrPNr.png]
It's not messy, it's how this is done. You could shorten it by one line, but that's about it
Code
with open("userpass.txt", "r") as f:
    txt = f.read()

for line in txt:
    line.strip("\n")
    username_str, password_str = line.split(",")

Also make sure to close your file f.close() if you don't open it using the "with" keyword, if you do Python closes the file for you.
[Image: 0DvFxnA.gif]
Donate: 3ABKRghau93ziRaDfVwiiwtjky3i1gLrKn
Alternatively, use configparser or import another py file containing the user/pass variables. Nonetheless, you should get into a habit of not hard coding creds. I prefer to either use arguments or input(). Or atleast hash them.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[C] by jBarrelNov 15, 2022 02:52 PM 8 325 Nov 15, 2022 02:52 PM
Last Post: HFDone
[C#] by GuillyianJul 11, 2022 05:19 AM 5 233 Jul 11, 2022 05:19 AM
Last Post: MavenGradle