[Tutor] A year's calendar to a text file?

Dick Moores rdm at rcblue.com
Sun Jul 4 17:02:32 EDT 2004


Here's the latest version of my script. I'm pleased that the script 
works, but I'd appreciate some tips on how I could do this in a more 
Pythonesque way.

And I'm particularly concerned about the section where I tell the user 
the name of the file created (Alan Gauld in his tutorial recommends this 
kind of user feedback). Isn't there a better or easier way to print the 
filename without the spaces that would appear if I'd used

print "\nYear", year, "calendar created as c", year, ".txt"

Maybe using something like backspace to eliminate the two unneeded 
spaces? I've tried experimenting with \b, but I can't get it to work. 
(I'm using Windows XP.)

The script prints (for 1942):
Year 1942 calendar created as c1942.txt
Full path is C:\Documents and Settings\Dick\Desktop\Year 
Calendars\cal1942.txt

This is pretty trivial, I suppose, but I'd like to know.

"""
# calendar_to_textfile.py
# create year calendar for year of user's choice (from 1 C.E. thru 9999 C.E.)

import calendar

# get from user the year for which he wants a calendar
year = raw_input("Enter year you want calendar for ")

# partial path for text file for calendar
s = "C:\Documents and Settings\Dick\Desktop\Year Calendars\cal"

# copy s to path
path = ""

for x in s:
     path += x

# add year to path
for x in year[0:4]:
     path += x

#add ".txt" to path
txt = ".txt"
for x in txt[0:4]:
     path += x

calendar.setfirstweekday(6) #sets first day of week to Sunday
inp = calendar.calendar(int(year))

outp = file(path,"w")

for line in inp:
     outp.write(line)

outp.close()

# build filename for printing
filename = "c"
for x in year:
     filename += x
for x in ".txt":
     filename += x

print "\nYear", year, "calendar created as", filename
print "Full path is", path
"""

Thanks, tutors,

Dick Moores





More information about the Tutor mailing list