Python Forum

Full Version: Database Creation and Reading
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, so I don't know if this is a stupid question or an impossible question but I'd just like to know if you guys can help me. I've decided to start developing a read/write program that stores user info. I was wondering if you knew how to make a script that will ask for someone's name and then create a file with the same name as them? And yes there will be further questions on this subject so all information would be really helpful, thanks! (Oh, and I'm sorry if I posted this in the wrong category, I wasn't really sure) Big Grin Big Grin Big Grin
Quote: I was wondering if you knew how to make a script that will ask for someone's name and then create a file with the same name as them?
Answer is yes
Could you tell me how to do that plz?
This is basic python, any tutorial will have you there probably within the first hour.
see: http://interactivepython.org/courselib/s...index.html
and: http://www.greenteapress.com/thinkpython...ython.html

Here's a simple example (untested) save as write_something.py:
def write_some_text(sometext, filename):
    with open(filename, 'w') as fp:
        fp.write(sometext)

write_some_text('Hey, what do you think about that', 'MyTextFile.txt')
you can run from command line with:
python write_something.py
To read back and print:
read_me.py:
def read_me(filename):
    with open(filename) as fp:
        read_text = fp.read()

print(read_me('MyTextFile.txt'))