Reading, editing and saving a txt file

Artur Sousa tucalipe at gmail.com
Wed Nov 5 12:57:07 EST 2008


Hi everyone.
What I'm trying to do is a Python script that reads a txt file with the
following structure:

> AI_Tactic.dat
>  NO_AI
>  1
>
> Animation.dat
>  Intro
>  2
>
> Areas.dat
>  Start
>  8


Then asks for a user input on what line containing one of those filenames
the script should read, show the desired line, the module and it's id
number. Then ask whether to replace with a new module, by only asking the
new module's name and writing to the txt file both the new module's name and
the new module's ID number which should be the last one + 1.

The desired results should be sth like:
chklst.txt: (before)

> AI_Tactic.dat
>   NO_AI
>   1
>
> Animation.dat
>
   Intro
>   2
>
> Areas.dat
>   Start
>   8
>


> The user then runs the script:

 Which file would you want to append a new module?
> Input>Animation.dat
> This file's last entry was "Intro", ID "2". Would you like to append a new
> module?
> Input>Y
> State the new module's name.
> Input>Outro
> The module "Outro" is now appended to the list with ID "3".
>

Then, chklst.txt would be saved and end up like:

>
> AI_Tactic.dat
>   NO_AI
>   1
>
> Animation.dat
>
   Outro
>   3
>
> Areas.dat
>   Start
>   8
>

----------------------------------------------

I've already started some programming, but I'm stuck.

file = open("chklst.txt")
for line in file:
    #That's where I'm stuck...
    pass
print "Which file would you want to append a new module to?
archive = (raw_input("Input>"))
#then it should find the desired module, and:
print "This file's last entry was"
print module #where module would be the module's name read by python
print "with ID number"
print idno #where idno would be the module's ID number
#I've not figured out how to print text and strings on the same line,
though.
print "Would you like to append a new module to the desired file? (Y or N)"
append = (raw_input("Input>"))
if append = "Y":
    pass
if append = "N":
    #it should then ask again for another file to be examined
    pass
print "Please state the new module's name"
name = (raw_input("Input>"))
#then it should replace the existing module on the file the user requested
with this new one.
print "The module " + name +" was succesfully appended with ID no " idno+1
#then the program finishes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081105/4857f319/attachment.html>


More information about the Python-list mailing list