Replacing lines in txt. files.

This is the place for queries that don't fit in any of the other categories.

Replacing lines in txt. files.

Postby HairyDrummerGuy » Tue Mar 19, 2013 2:42 pm

Hey, this is my first post :D
I am fairly experienced with python. Been using it for a few months as a computer science student.

I am working on a text-based horror survival game. I didn't post this in the games development section because the problem and it's solution are not hugely related to the game itself, more it's system.

I worked out a system were the game loads a txt. and reads it to find out were the user was when they left off. (Putting it fairly simply, I have a save/load feature)

Anyways, the txt. contains just a single line, containing a number. I have got the load section working, but I still need to get the save working. How can I replace the first line on the txt. file? Or maybe just replace the whole thing, as it is just one line.

Thanks for the help :D I didn't post any code as it seems simple enough. I just need to know a command.
HairyDrummerGuy
 
Posts: 6
Joined: Tue Mar 19, 2013 2:30 pm

Re: Replacing lines in txt. files.

Postby setrofim » Tue Mar 19, 2013 3:01 pm

Hi, welcome to the forum!

Normally, you would need to read the contents of the file in, modify/replace the first line, and then write it all back. You can't really modify just the first line in a file on disk (unless the modification doesn't change the byte size of the file, in which case you could do it but it's still not recommended).

I your case though, the simplest thing is to just overwrite the file with the new value.
Last edited by setrofim on Tue Mar 19, 2013 3:02 pm, edited 1 time in total.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Replacing lines in txt. files.

Postby Mekire » Tue Mar 19, 2013 3:01 pm

HairyDrummerGuy wrote:I am fairly experienced with python
Um... then what exactly is the problem here. I'm a bit confused. Open the file (preferably using with/as) and read the data to load. To save simply open the same file in write mode and write the new data (if it really is one line it should be trivial).

-Mek
Last edited by Mekire on Tue Mar 19, 2013 3:03 pm, edited 1 time in total.
User avatar
Mekire
 
Posts: 1033
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Replacing lines in txt. files.

Postby metulburr » Tue Mar 19, 2013 3:02 pm

I didn't post this in the games development section because the problem and it's solution are not hugely related to the game itself, more it's system.

Im glad you did. Most people think if their program in in tkinter then it must be a problem with that, but it really is a general python problem nothing related to it.

I worked out a system were the game loads a txt. and reads it to find out were the user was when they left off. (Putting it fairly simply, I have a save/load feature)

Anyways, the txt. contains just a single line, containing a number. I have got the load section working, but I still need to get the save working. How can I replace the first line on the txt. file? Or maybe just replace the whole thing, as it is just one line.

Normally what ends up happening is you eventually need to save more and more data. And you can either do each peice of data as a separate file or recode. Its better to recode and adjust for the addition than to just keep using the text method. So as you might need only one number to save now, later you might need 2 numbers, a float, a string, a dictionary, or a list, etc.

So long story short use json, shelve, or pickle, or some other similar method. So then you have a dictionary to load/save, and can add/delete to it, and not have to modify code to accomodate the change.

But to finally answer your question. With you only saving one number on line. I would just open the file in 'w' mode and redo the entire thing.

I am guessing you were asking about the line numbers because you see in the future that each line is a different piece of data? Yeah tried that also, its possible, not too hard, but the other methods make it a breeze. So in case this was your idea, just giving you a heads up

I am fairly experienced with python. Been using it for a few months as a computer science student.

I would not consider a few months fairly experienced. I have been doing python almost 2 years and i do not considered myself to be anywhere near that title yet.

A fairly experienced python programmer would have multiple methods of saving to and from a file. With the experience of doing so already. They would not be asking how to save/load data to and from a text file. They also would not really be programming a text based game (mostly not, not really set in stone though) . They would have already done so and most likely already programmed one or even two text based games by now. They would be more apt to be programming a 2d game or 3d game using a 3rd party library, for example.

I also have a fissy when people graduate college and consider themselves an expert just because they have a piece of paper. When in actuality they are raw in the field, very raw. Ok i am done with my rant. I just really dont like when people say they are someone who they are really not.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1513
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Replacing lines in txt. files.

Postby Mekire » Tue Mar 19, 2013 3:52 pm

Yeah. I agree with Metul on this though perhaps I came off too harsh. The, "I'm fairly experienced," line just gets under my skin (I am also miles from this title). For what it's worth I also detest when people write, "I'm a complete n00b." Neither preface is necessary. The difficulty of what you are asking and the difficulty of what you have done will speak for itself.

Anyway:
Code: Select all
#make a variable with a value.
print("Assign a variable a value in your code.")
data = 5
print("Data = {}".format(data))

#open a file and write it.
print("\nOpen a file and write the data.")
with open("mydata.txt",'w') as myfile:
    myfile.write(str(data))

#Change the value
print("\nSet the data to something else.")
data = 7
print("Data = {}".format(data))

#load the file
print("\nLoad the file and retreive the data")
with open("mydata.txt",'r') as myfile:
    data = int(myfile.readline(data).strip())
print("Data = {}".format(data))
Using basics.

As Metul suggested however you should probably look into using pickle or similar. This will avoid having to parse the data back to the type you want.

-Mek
User avatar
Mekire
 
Posts: 1033
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Replacing lines in txt. files.

Postby HairyDrummerGuy » Tue Mar 19, 2013 7:26 pm

Mekire wrote:Yeah. I agree with Metul on this though perhaps I came off too harsh. The, "I'm fairly experienced," line just gets under my skin (I am also miles from this title). For what it's worth I also detest when people write, "I'm a complete n00b." Neither preface is necessary. The difficulty of what you are asking and the difficulty of what you have done will speak for itself.


I just meant I have a tiny bit of knowledge xD I phrased it badly. I am no way near and expert!
Last edited by Yoriz on Tue Mar 19, 2013 7:52 pm, edited 1 time in total.
Reason: Fixed the quote
HairyDrummerGuy
 
Posts: 6
Joined: Tue Mar 19, 2013 2:30 pm

Re: Replacing lines in txt. files.

Postby HairyDrummerGuy » Tue Mar 19, 2013 7:28 pm

metulburr wrote:
I didn't post this in the games development section because the problem and it's solution are not hugely related to the game itself, more it's system.

Im glad you did. Most people think if their program in in tkinter then it must be a problem with that, but it really is a general python problem nothing related to it.

I worked out a system were the game loads a txt. and reads it to find out were the user was when they left off. (Putting it fairly simply, I have a save/load feature)

Anyways, the txt. contains just a single line, containing a number. I have got the load section working, but I still need to get the save working. How can I replace the first line on the txt. file? Or maybe just replace the whole thing, as it is just one line.

Normally what ends up happening is you eventually need to save more and more data. And you can either do each peice of data as a separate file or recode. Its better to recode and adjust for the addition than to just keep using the text method. So as you might need only one number to save now, later you might need 2 numbers, a float, a string, a dictionary, or a list, etc.

So long story short use json, shelve, or pickle, or some other similar method. So then you have a dictionary to load/save, and can add/delete to it, and not have to modify code to accomodate the change.

But to finally answer your question. With you only saving one number on line. I would just open the file in 'w' mode and redo the entire thing.

I am guessing you were asking about the line numbers because you see in the future that each line is a different piece of data? Yeah tried that also, its possible, not too hard, but the other methods make it a breeze. So in case this was your idea, just giving you a heads up

I am fairly experienced with python. Been using it for a few months as a computer science student.

I would not consider a few months fairly experienced. I have been doing python almost 2 years and i do not considered myself to be anywhere near that title yet.

A fairly experienced python programmer would have multiple methods of saving to and from a file. With the experience of doing so already. They would not be asking how to save/load data to and from a text file. They also would not really be programming a text based game (mostly not, not really set in stone though) . They would have already done so and most likely already programmed one or even two text based games by now. They would be more apt to be programming a 2d game or 3d game using a 3rd party library, for example.

I also have a fissy when people graduate college and consider themselves an expert just because they have a piece of paper. When in actuality they are raw in the field, very raw. Ok i am done with my rant. I just really dont like when people say they are someone who they are really not.


Thanks, I will keep this in mind. This is only a simple program (but your information may come in handy in other games I make in the future)
And when i said 'fairly experienced' I just meant I know a few things. Phrased it badly xD
HairyDrummerGuy
 
Posts: 6
Joined: Tue Mar 19, 2013 2:30 pm

Re: Replacing lines in txt. files.

Postby tnknepp » Thu Mar 21, 2013 3:36 pm

metulburr wrote:I also have a fissy when people graduate college and consider themselves an expert just because they have a piece of paper. When in actuality they are raw in the field, very raw. Ok i am done with my rant. I just really dont like when people say they are someone who they are really not.


AND you are nicotine deprived, which doesn't help. :)
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 134
Joined: Mon Mar 11, 2013 7:41 pm

Re: Replacing lines in txt. files.

Postby metulburr » Thu Mar 21, 2013 5:06 pm

lol yeah i have been an asshole since i quit
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1513
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests