can someone give me some tips on how to make Python programs that write data to external files?
-
so lately i've been learning about making Python programs that write to text files. and I made an example of such (in this we assume in the directory theres a file called "database.txt")
file = open("database.txt","r+") name = input("Enter your name: ") age = int(input("Enter your age: ")) def is_minor(age): is_minor = False if age > 18: is_minor = True else: is_minor = False return is_minor minor = is_minor(age) def write_to_file(name, age, file, minor): file.write(name + ", " file.write(str(age) + ", ") file.write(minor) write_to_file(name, age, file, minor) file.close()and now my problem with this is: it does actually work, it writes allow 4 values to the txt file, but everytime it runs it gets rid of what was there before. I don't know if that means that i'm using the wrong file.open("database.txt,"r+"). maybe the "r+" isn't for appending. honestly the problem is likely the file.write statement, so if anyone knows what file.write() statement i could use to append information to the file without overriding the contents, that would be amazing.
-
1. Wrong logic in
is_minor()You wrote:
if age > 18: is_minor = True else: is_minor = FalseThis means someone over 18 is considered a minor, which is backwards.
It should be:if age < 18: is_minor = True else: is_minor = FalseOr even shorter:
return age < 18
2. Missing parenthesis in
file.write()In
write_to_file():file.write(name + ", "You forgot the closing
)for thatfile.writecall.
3. Writing a boolean directly to a file
file.write()needs a string.
You’re trying to write:file.write(minor)But
minoris a boolean (TrueorFalse). You must convert it to string:file.write(str(minor))
4. You’re opening the file with
r+r+means read/write without truncating. If the file doesn’t exist, it will throw an error. If you just want to append, use"a"or"a+". If you want to overwrite, use"w".
Fixed Code
Here’s a working version:
# Open in append mode file = open("database.txt", "a") name = input("Enter your name: ") age = int(input("Enter your age: ")) def is_minor(age): return age < 18 minor = is_minor(age) def write_to_file(name, age, file, minor): file.write(name + ", ") file.write(str(age) + ", ") file.write(str(minor) + "\n") write_to_file(name, age, file, minor) file.close() -
1. Wrong logic in
is_minor()You wrote:
if age > 18: is_minor = True else: is_minor = FalseThis means someone over 18 is considered a minor, which is backwards.
It should be:if age < 18: is_minor = True else: is_minor = FalseOr even shorter:
return age < 18
2. Missing parenthesis in
file.write()In
write_to_file():file.write(name + ", "You forgot the closing
)for thatfile.writecall.
3. Writing a boolean directly to a file
file.write()needs a string.
You’re trying to write:file.write(minor)But
minoris a boolean (TrueorFalse). You must convert it to string:file.write(str(minor))
4. You’re opening the file with
r+r+means read/write without truncating. If the file doesn’t exist, it will throw an error. If you just want to append, use"a"or"a+". If you want to overwrite, use"w".
Fixed Code
Here’s a working version:
# Open in append mode file = open("database.txt", "a") name = input("Enter your name: ") age = int(input("Enter your age: ")) def is_minor(age): return age < 18 minor = is_minor(age) def write_to_file(name, age, file, minor): file.write(name + ", ") file.write(str(age) + ", ") file.write(str(minor) + "\n") write_to_file(name, age, file, minor) file.close()@Links Thank you so much! I see what your saying, so I need to fix the is_minor logic, I'm forgetting the ")" in the file.write statement, and I need to converrt the Bool from is_minor into a string. And also, thank you you telling me to open it with "a+", I was very confused with that. Thank you again for your help!
-
@Links Thank you so much! I see what your saying, so I need to fix the is_minor logic, I'm forgetting the ")" in the file.write statement, and I need to converrt the Bool from is_minor into a string. And also, thank you you telling me to open it with "a+", I was very confused with that. Thank you again for your help!
-
theres always chatgpt

-
theres always chatgpt

@Andyboi yeah actually but he sometimes is outdated so its better to just check the forms
-
@Links exactly, plus chatgpt doesn't even work that well on the switch
-
@Links exactly, plus chatgpt doesn't even work that well on the switch
@danniltrifonov Yeah and chat gpt cant even write code. it can just correct code
-
ive heard chatgpt can write code
-
ive heard chatgpt can write code
@Andyboi it can but it always glitches and gives LOADS OF error messages. (The code)