0
def new_pass():
    User_new = input('Enter Username: ')
    Pass_new = input('Enter Password: ')
    output_final = User_new + ' ' + Pass_new
    with open('new.txt', 'w') as new_folder:
        new_folder.write(output_final)
    return 'Done'

new_pass()
allthelines = open("new.txt")
allthelines = allthelines.split()

This is how I am trying to use one function; but when I'm rerunning it it is not letting me access the data.

I want the text file to be like this:

string 1
string 2
string 3, string 4
string 5, string 6

This is the output I am getting:

Enter Username: Example
Enter Password: Example2
Traceback (most recent call last):
  File "/Users/kavneet/Desktop/Project/maincode.py", line 10, in <module>
    allthelines = allthelines.split()
AttributeError: '_io.TextIOWrapper' object has no attribute 'split'
(base) Kavneets-MacBook-Air:~ kavneet$ 

I want it to be a dict of lines like the 3rd and 4th. How can I do this? Also how do I insert text one the next line. Any help is appreciated!

Samrath
  • 29
  • 1
  • 1
  • 6
  • What is the current result you got? By a brief the file will have only one line at the end, because the function invoked only once, secondly, if you want it to append to the file, dont use `w` as a flag but `a` which stands for append. Another mismatch is that in the snippet you wrote with spacebar as delimeter and in expected `,` is the delimeter – Yossi Levi Oct 07 '20 at 09:11
  • You called .split on a file, I guess youre trying .read().split(), anyway you can read file lome by .readlines(). On the file you want, user, password separated by comma? – geckos Oct 07 '20 at 11:14

0 Answers0