Date using input

F

flebber

Sorry to ask a simple question but I am a little confused how to
combine the input function and the date time module.

Simply at the start of the program I want to prompt the user to enter
the date, desirably in the format dd/mm/year.
However I want to make sure that python understands the time format
because first the date will form part of the name of the output file
so dd/mm/year as 1st September 2009, secondly if I have multiple
output files saved in a directory I may need to search later on the
files and contents of files by date range. So can I timestamp the
file?

I know this is a simple question but it eludes me exactly how to do
it.

I have the basics from http://docs.python.org/library/datetime.html

from datetime import date
date = input("type date dd/mm/year: ")
datetime(day,month,year)

# some program blocks

#print to file(name = date) or apphend if it exists
 
D

Dave Angel

flebber said:
Sorry to ask a simple question but I am a little confused how to
combine the input function and the date time module.

Simply at the start of the program I want to prompt the user to enter
the date, desirably in the format dd/mm/year.
However I want to make sure that python understands the time format
because first the date will form part of the name of the output file
so dd/mm/year as 1st September 2009, secondly if I have multiple
output files saved in a directory I may need to search later on the
files and contents of files by date range. So can I timestamp the
file?

I know this is a simple question but it eludes me exactly how to do
it.

I have the basics from http://docs.python.org/library/datetime.html

from datetime import date
date = input("type date dd/mm/year: ")
datetime(day,month,year)

# some program blocks

#print to file(name = date) or apphend if it exists
What version of python is your class, instructor, and text book using?
If you want to learn fastest, you probably need to be using the same, or
nearly same environment. The input() function is one place where it
matters whether it's Python 2.x or Python 3.x. While you're at it, you
should give the rest of your environment, such as which OS.

The doc page you pointed us to is for Python 2.6.2, but the input
function on that version returns an integer. Perhaps you want raw_input() ?

What code have you written, and what about it doesn't work? Have you
added print statements before the line that causes the error to see what
the intermediate values are?


To try to anticipate some of your problems, you should realize that in
most file systems, the slash is a reserved character, so you can't write
the date that way. I'd suggest using dashes. I put dates in directory
names, and I always put year, then month, then day, because then sorting
the filenames also sorts the dates. I'm not in a country that sorts
dates that way, but it does make things easier. So directories for the
last few days would be:
2009-09-22
2009-09-23
2009-09-24

When asking the user for a date, or telling him a date, by all means use
your country's preferred format, as you say.

You mention timestamping the file. While that can be done (Unix touch,
for example), I consider it a last resort for keeping track of useful
information. At best, it should be an extra "reminder" of something
that's already in the file contents. And since many programs make the
assumption that if the timestamp doesn't change, the contents haven't
changed, you can only reasonably do this on a file whose contents are
fixed when first created.

If you control the internal format of the file, put the date there,
perhaps right after the header which defines the data type and version.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top