Checking if string exists in file using while loop












0














I am new to Python and am having trouble with looping while checking strings in a file. For this program, I am checking to see if a username that the user wants to create already exists. If the username already exists in the file, the program prompts the user to enter another username. The loop ends when the user enters a username that is not in the file. Here is the relevant code:



# Prompting for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")

# open password file
f = open("password.txt", "r")

# while username exists in file
while username in f.read():
username = input("Enter your username: ")

f.close()


If I enter a username that exists in the password file, the program does prompt me to enter another username; however, when I enter the same username, the program does not stay in the loop. Any ideas on why this is occurring?










share|improve this question






















  • Is this all the code or have you given a minimalist version?
    – Amit Joki
    Nov 20 at 5:53










  • How do the contents of password.txt look like?
    – user7374610
    Nov 20 at 5:54










  • Thanks everybody!
    – CMoney
    Nov 20 at 23:28
















0














I am new to Python and am having trouble with looping while checking strings in a file. For this program, I am checking to see if a username that the user wants to create already exists. If the username already exists in the file, the program prompts the user to enter another username. The loop ends when the user enters a username that is not in the file. Here is the relevant code:



# Prompting for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")

# open password file
f = open("password.txt", "r")

# while username exists in file
while username in f.read():
username = input("Enter your username: ")

f.close()


If I enter a username that exists in the password file, the program does prompt me to enter another username; however, when I enter the same username, the program does not stay in the loop. Any ideas on why this is occurring?










share|improve this question






















  • Is this all the code or have you given a minimalist version?
    – Amit Joki
    Nov 20 at 5:53










  • How do the contents of password.txt look like?
    – user7374610
    Nov 20 at 5:54










  • Thanks everybody!
    – CMoney
    Nov 20 at 23:28














0












0








0







I am new to Python and am having trouble with looping while checking strings in a file. For this program, I am checking to see if a username that the user wants to create already exists. If the username already exists in the file, the program prompts the user to enter another username. The loop ends when the user enters a username that is not in the file. Here is the relevant code:



# Prompting for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")

# open password file
f = open("password.txt", "r")

# while username exists in file
while username in f.read():
username = input("Enter your username: ")

f.close()


If I enter a username that exists in the password file, the program does prompt me to enter another username; however, when I enter the same username, the program does not stay in the loop. Any ideas on why this is occurring?










share|improve this question













I am new to Python and am having trouble with looping while checking strings in a file. For this program, I am checking to see if a username that the user wants to create already exists. If the username already exists in the file, the program prompts the user to enter another username. The loop ends when the user enters a username that is not in the file. Here is the relevant code:



# Prompting for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")

# open password file
f = open("password.txt", "r")

# while username exists in file
while username in f.read():
username = input("Enter your username: ")

f.close()


If I enter a username that exists in the password file, the program does prompt me to enter another username; however, when I enter the same username, the program does not stay in the loop. Any ideas on why this is occurring?







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 5:45









CMoney

32




32












  • Is this all the code or have you given a minimalist version?
    – Amit Joki
    Nov 20 at 5:53










  • How do the contents of password.txt look like?
    – user7374610
    Nov 20 at 5:54










  • Thanks everybody!
    – CMoney
    Nov 20 at 23:28


















  • Is this all the code or have you given a minimalist version?
    – Amit Joki
    Nov 20 at 5:53










  • How do the contents of password.txt look like?
    – user7374610
    Nov 20 at 5:54










  • Thanks everybody!
    – CMoney
    Nov 20 at 23:28
















Is this all the code or have you given a minimalist version?
– Amit Joki
Nov 20 at 5:53




Is this all the code or have you given a minimalist version?
– Amit Joki
Nov 20 at 5:53












How do the contents of password.txt look like?
– user7374610
Nov 20 at 5:54




How do the contents of password.txt look like?
– user7374610
Nov 20 at 5:54












Thanks everybody!
– CMoney
Nov 20 at 23:28




Thanks everybody!
– CMoney
Nov 20 at 23:28












3 Answers
3






active

oldest

votes


















0














When you run f.read() Python will read the file in and then continue onto the next line of the file in the next iteration. It wont go back to the top of the file. So it exits the loop since the username in the next line of the file is either an empty string or another name. To fix this you could use a context manager like so:



# Prompting for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")

# read in the file data
with open('password.txt') as f:
data = f.read()

# while username exists in file
while username in data:
username = input("Enter your username: ")


Then depending on how the data is structured in your .txt file then you can call split() on data if it's using a new line.






share|improve this answer























  • This worked! Thank you!
    – CMoney
    Nov 20 at 23:27










  • @CMoney stackoverflow.com/help/someone-answers "Please do not add a comment on your question or on an answer to say "Thank you". ", etc. Vote and/or accept. thanks.
    – davedwards
    Nov 21 at 4:13





















0














There is no condition to check whether the new username is in the file or not.



Maybe an easier way to do this would be to use the following approach?



username = input("Enter your username: ")
password = input("Enter your password: ")

# open password file
f = open("password.txt", "r")
data = f.read()

# while username exists in file
while username in data:
new = input("Enter your username: ")
if new in data:
continue
else:
break

username = new
f.close()





share|improve this answer























  • Thanks for the feedback!
    – CMoney
    Nov 20 at 23:27



















0














This is because you are using f.read() in the while condition.
f.read reads the whole content of the file at once and there's nothing else to read from file, leading to the end of while loop.



If you want to check for usernames in the file, I'd suggest you create a list of usernames that you read from the file and use it in your while loop for checking.



If your file has contents:
username1, username2, ...



You can do



listOfUsernames = f.read().split(',')


and then use this for checking in while loop.






share|improve this answer





















  • Thanks for the help!
    – CMoney
    Nov 20 at 23:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386920%2fchecking-if-string-exists-in-file-using-while-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














When you run f.read() Python will read the file in and then continue onto the next line of the file in the next iteration. It wont go back to the top of the file. So it exits the loop since the username in the next line of the file is either an empty string or another name. To fix this you could use a context manager like so:



# Prompting for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")

# read in the file data
with open('password.txt') as f:
data = f.read()

# while username exists in file
while username in data:
username = input("Enter your username: ")


Then depending on how the data is structured in your .txt file then you can call split() on data if it's using a new line.






share|improve this answer























  • This worked! Thank you!
    – CMoney
    Nov 20 at 23:27










  • @CMoney stackoverflow.com/help/someone-answers "Please do not add a comment on your question or on an answer to say "Thank you". ", etc. Vote and/or accept. thanks.
    – davedwards
    Nov 21 at 4:13


















0














When you run f.read() Python will read the file in and then continue onto the next line of the file in the next iteration. It wont go back to the top of the file. So it exits the loop since the username in the next line of the file is either an empty string or another name. To fix this you could use a context manager like so:



# Prompting for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")

# read in the file data
with open('password.txt') as f:
data = f.read()

# while username exists in file
while username in data:
username = input("Enter your username: ")


Then depending on how the data is structured in your .txt file then you can call split() on data if it's using a new line.






share|improve this answer























  • This worked! Thank you!
    – CMoney
    Nov 20 at 23:27










  • @CMoney stackoverflow.com/help/someone-answers "Please do not add a comment on your question or on an answer to say "Thank you". ", etc. Vote and/or accept. thanks.
    – davedwards
    Nov 21 at 4:13
















0












0








0






When you run f.read() Python will read the file in and then continue onto the next line of the file in the next iteration. It wont go back to the top of the file. So it exits the loop since the username in the next line of the file is either an empty string or another name. To fix this you could use a context manager like so:



# Prompting for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")

# read in the file data
with open('password.txt') as f:
data = f.read()

# while username exists in file
while username in data:
username = input("Enter your username: ")


Then depending on how the data is structured in your .txt file then you can call split() on data if it's using a new line.






share|improve this answer














When you run f.read() Python will read the file in and then continue onto the next line of the file in the next iteration. It wont go back to the top of the file. So it exits the loop since the username in the next line of the file is either an empty string or another name. To fix this you could use a context manager like so:



# Prompting for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")

# read in the file data
with open('password.txt') as f:
data = f.read()

# while username exists in file
while username in data:
username = input("Enter your username: ")


Then depending on how the data is structured in your .txt file then you can call split() on data if it's using a new line.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 6:11

























answered Nov 20 at 6:04









kstullich

213318




213318












  • This worked! Thank you!
    – CMoney
    Nov 20 at 23:27










  • @CMoney stackoverflow.com/help/someone-answers "Please do not add a comment on your question or on an answer to say "Thank you". ", etc. Vote and/or accept. thanks.
    – davedwards
    Nov 21 at 4:13




















  • This worked! Thank you!
    – CMoney
    Nov 20 at 23:27










  • @CMoney stackoverflow.com/help/someone-answers "Please do not add a comment on your question or on an answer to say "Thank you". ", etc. Vote and/or accept. thanks.
    – davedwards
    Nov 21 at 4:13


















This worked! Thank you!
– CMoney
Nov 20 at 23:27




This worked! Thank you!
– CMoney
Nov 20 at 23:27












@CMoney stackoverflow.com/help/someone-answers "Please do not add a comment on your question or on an answer to say "Thank you". ", etc. Vote and/or accept. thanks.
– davedwards
Nov 21 at 4:13






@CMoney stackoverflow.com/help/someone-answers "Please do not add a comment on your question or on an answer to say "Thank you". ", etc. Vote and/or accept. thanks.
– davedwards
Nov 21 at 4:13















0














There is no condition to check whether the new username is in the file or not.



Maybe an easier way to do this would be to use the following approach?



username = input("Enter your username: ")
password = input("Enter your password: ")

# open password file
f = open("password.txt", "r")
data = f.read()

# while username exists in file
while username in data:
new = input("Enter your username: ")
if new in data:
continue
else:
break

username = new
f.close()





share|improve this answer























  • Thanks for the feedback!
    – CMoney
    Nov 20 at 23:27
















0














There is no condition to check whether the new username is in the file or not.



Maybe an easier way to do this would be to use the following approach?



username = input("Enter your username: ")
password = input("Enter your password: ")

# open password file
f = open("password.txt", "r")
data = f.read()

# while username exists in file
while username in data:
new = input("Enter your username: ")
if new in data:
continue
else:
break

username = new
f.close()





share|improve this answer























  • Thanks for the feedback!
    – CMoney
    Nov 20 at 23:27














0












0








0






There is no condition to check whether the new username is in the file or not.



Maybe an easier way to do this would be to use the following approach?



username = input("Enter your username: ")
password = input("Enter your password: ")

# open password file
f = open("password.txt", "r")
data = f.read()

# while username exists in file
while username in data:
new = input("Enter your username: ")
if new in data:
continue
else:
break

username = new
f.close()





share|improve this answer














There is no condition to check whether the new username is in the file or not.



Maybe an easier way to do this would be to use the following approach?



username = input("Enter your username: ")
password = input("Enter your password: ")

# open password file
f = open("password.txt", "r")
data = f.read()

# while username exists in file
while username in data:
new = input("Enter your username: ")
if new in data:
continue
else:
break

username = new
f.close()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 6:08

























answered Nov 20 at 5:58









Rupaksh Paul

488




488












  • Thanks for the feedback!
    – CMoney
    Nov 20 at 23:27


















  • Thanks for the feedback!
    – CMoney
    Nov 20 at 23:27
















Thanks for the feedback!
– CMoney
Nov 20 at 23:27




Thanks for the feedback!
– CMoney
Nov 20 at 23:27











0














This is because you are using f.read() in the while condition.
f.read reads the whole content of the file at once and there's nothing else to read from file, leading to the end of while loop.



If you want to check for usernames in the file, I'd suggest you create a list of usernames that you read from the file and use it in your while loop for checking.



If your file has contents:
username1, username2, ...



You can do



listOfUsernames = f.read().split(',')


and then use this for checking in while loop.






share|improve this answer





















  • Thanks for the help!
    – CMoney
    Nov 20 at 23:27
















0














This is because you are using f.read() in the while condition.
f.read reads the whole content of the file at once and there's nothing else to read from file, leading to the end of while loop.



If you want to check for usernames in the file, I'd suggest you create a list of usernames that you read from the file and use it in your while loop for checking.



If your file has contents:
username1, username2, ...



You can do



listOfUsernames = f.read().split(',')


and then use this for checking in while loop.






share|improve this answer





















  • Thanks for the help!
    – CMoney
    Nov 20 at 23:27














0












0








0






This is because you are using f.read() in the while condition.
f.read reads the whole content of the file at once and there's nothing else to read from file, leading to the end of while loop.



If you want to check for usernames in the file, I'd suggest you create a list of usernames that you read from the file and use it in your while loop for checking.



If your file has contents:
username1, username2, ...



You can do



listOfUsernames = f.read().split(',')


and then use this for checking in while loop.






share|improve this answer












This is because you are using f.read() in the while condition.
f.read reads the whole content of the file at once and there's nothing else to read from file, leading to the end of while loop.



If you want to check for usernames in the file, I'd suggest you create a list of usernames that you read from the file and use it in your while loop for checking.



If your file has contents:
username1, username2, ...



You can do



listOfUsernames = f.read().split(',')


and then use this for checking in while loop.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 6:09









Hardhik

367




367












  • Thanks for the help!
    – CMoney
    Nov 20 at 23:27


















  • Thanks for the help!
    – CMoney
    Nov 20 at 23:27
















Thanks for the help!
– CMoney
Nov 20 at 23:27




Thanks for the help!
– CMoney
Nov 20 at 23:27


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386920%2fchecking-if-string-exists-in-file-using-while-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?