How to store user input in txt file?(Python 3) [duplicate] - python

This question already has answers here:
.write not working in Python
(5 answers)
Closed 6 years ago.
So i am writing a program and i made a function which stores user data in a txt file.It is like a simple registration function.The user gives the username and password to store.Problem is i can't store user input in the file.
My code is :
def reguser(): #Function name.
fwu = open('user.txt','w') #create or open user.txt file.
user = input("Username: ") #takes the user input.e.g what username to register
fwu.write(user) #this command should write input into the file
fwp = open('pass.txt','w') #create or open pass.txt file.
pas = input ("Password: ") #takes user input e.g what password to register
fwp.write(pas) #write the password into the file
print ("To check for registraion completion, please login.")
askuser()
So what i get is two text files user and pass but they are empty.
What am i doing wrong??
and please do not tell me to use modules for registraion.
Regards ali7112001

You didn't fwu.close() or fwp.close() (you didn't save it). Also a quick look up next time would save you some time. .write not working in Python

Please try with raw_input() instead of input()
Also please close() the files.

Related

How can I use an "if statement" to check if what a user has inputted is the same as text on another file? (Python)

I am making something that is password protected and to stop you from being able to see the password just by looking at the code I put it on another file and I am trying to have the code check if what was input is the same as the password on the other file
I have a variable that reads the file with the password and a different variable that reads what the user input and when I make an if statement that checks if the user input variable is the same as what is on the other file but if I input the correct password to the variable that gets checked against the other file it doesnt say I put the right one in

How do I record text entered in the prompt function of pyautogui?

I'm writing a program, and I want to make users type feedback in the form of a prompt function in pyautogui. Is there any way I can record the text entered here; preferably in a text file?
Their docs say that pyautogui.prompt returns the text input or None if users hit cancel. Writing the text input to a file can be done like this:
import pyautogui as py
text = py.prompt(text='Do you like apples?', title='Question', default='YES')
with open('file.txt', 'w') as file:
file.writelines(text)
More easily you can just save the calling of prompt function to a variable,like:
import pyautogui
text = pyautogui.prompt(text='Do you like apples?', title='Question', default='YES')
#now you will have the text entered in the prompt saved in 'text' variable
print(text)

Python, how to check if the first element of a row is repeated on a csv doc

I am doing a simple password manager in python, the user needs to input platform, user and password.
I tryied to fill the csv in case it was because the csv was empty, also i defined both variables as a list.
I keep getting an error while trying to check if the input platform is already in the csv file.
This is the part of the code:
if n=="1":
fo=open(reqfile,'r')
data=csv.reader(fo)
datatoread=list(data)
while check==False:
newline.append(input("introduce platform:").lower)
for x in range(len(datatoread)-1):
if newline[0]==datatoread[x[0]]:
print("You already setup an username and password for this platform, please change the platform")
check=False
else:
check=True
The output when i print(newline) just after the append statement:
[<built-in method lower of str object at 0x037AF680>]
This is where i put the break:
fo=open(reqfile,'r')
data=csv.reader(fo)
datatoread=list(data)
while check==False:
newline.append(input("introduce platform:"))
for x in range(len(datatoread)):
print(newline)
print(datatoread)
if newline[0]==datatoread[x[0]]:
print("You already setup an username and password for this platform, please change the name of the platform")
check=False
newline.pop()
break
else:
check=True
break
The output is:
['youtube']
[['Platform', 'Username', 'Password']]
Also with the same error
The error:
File "/PassMan.py", line 27, in
if newline[0]==datatoread[x[0]]:
TypeError: 'int' object is not subscriptable
If you need all the code, there's my github repo:
https://github.com/DavidGarciaRicou/PasswordManagerPython
I advise you to use pandas to read and write your CSV file. I believe it will help you to handle this file type.
This answer shows how to check if there is a string inside a pandas dataframe (i.e. your CSV file).
Thanks everyone for the help.
I resolved the problem with this code, still don't know what i was doing wrong:
while check==False:
newline.append(input("Introduce platform:"))
for x in range(0,len(datatoread)):
platcheck=datatoread[x]
plattochk=platcheck[0]
if newline[0]==plattochk:
print("You already setup an username and password for this platform, please change the name of the platform")
check=False
newline.pop()
break
try:
if newline[0]!=plattochk:
check=True
except:
pass
fo.close

Autocomplete filename in python script [duplicate]

This question already has answers here:
Tab completion in Python's raw_input()
(4 answers)
Closed 4 years ago.
I have made a python script that asks the user to enter 2 or 3 filenames, which should be analysed. The filename insertion is done in the script (it is not passed as argument to argparse, because there are other choices that user has to make before that). Due to the company naming convention these filenames can be quite long and thus cumbersome to type. To help the user I am printing the contents of directory. For now I am using something like this:
fname = raw_input("Insert phase 1 filename: ")
(than I check the if file exists etc...)
Is there a way to implement autocomplete for filenames inside the python script by writing custom input() function?
Note that the script must run on different machines / OS and I can not ask users to install some non-standard python libraries.
If there is no clean way to do it I might use less fancy solution of just printing a number before the filenames in the directory and than ask the user to insert just the number.
This might help:
Tab completion in Python's raw_input()
If you don't want to use that and just "guess" the file:
commands = ["cute_file", "awesome_file"]
def find_file(text):
options = [i for i in commands if i.startswith(text)]
if len(options):
return options
else:
return None
my_input = input("File name:")
print(find_file(my_input))

How to get python to automatically write and save files

Is it possible to write a code in python that saves and writes files automatically?
How I want the code to work:
The user opens the program. The program asks for an input, lets say it asks for the users name. User inputs his name. The program then takes that name and writes it in an external file and saves. The user then closes the program. Next time the program is opened it imports the external file, thus knowing the name of the user.
Yes of corse this are here a same introduction with some lines
import os
def write_to_file(name):
text_file = open("%s.txt"% name , "w")
text_file.write("%s" % name)
text_file.close()
def check_exists(name):
if os.path.exists("./"+name+".txt"):
print "File found!"
else:
write_to_file(name)
def input_name():
return(raw_input("input name: "))
check_exists(input_name())

Categories

Resources