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

Lloyd Kvam pythonTutor at venix.com
Mon Jul 5 19:15:35 CEST 2004


On Sun, 2004-07-04 at 17:02, Dick Moores wrote:
> 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"

Use the string format operator:
	print "\nYear %s calendar created as c%s.txt" % (year, year)

(snipped)
> """
> # 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"

For DOS/Windows paths, you will usually be better served with raw
strings:
s = r"C:\Documents and Settings\Dick\Desktop\Year Calendars\cal"

The backslash is used to mark "special" characters like backspace,
return, and linefeed.  s = "C:\boot" will NOT refer to your boot
directory!

> 
> # copy s to path
> path = ""
> 
> for x in s:
>      path += x
> 
You can simply bind s to path:
path = s

If it was essential for path to refer to a copy of s, then:
path = s[:] # uses the slice notation to create copy

would be better than copying one character at a time.  Since strings are
immutable, it is very rare for you to need to explicitly copy a string.


> # 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

Again there is no need to work one character at a time:
path = path + year + ".txt"

If you were sticking with the += operator:
path += year
path += ".txt"


> 
> 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)

In general, you could simply write the whole calendar at once:
outp.write(inp)


> 
> outp.close()
> 
> # build filename for printing
> filename = "c"
> for x in year:
>      filename += x
> for x in ".txt":
>      filename += x

Repeating the same logic in multiple spots can often lead to bugs from
typos or failures to change all spots together.  I'd recommend using
os.path.basename to extract the filename from path:
filename = os.path.basename(path)

Looking back at your code, I do not believe that the filename you're
printing is actually the same as the basename, but I think that was your
intent.  Should this piece have really started with:
filename = "cal"
?


> 
> print "\nYear", year, "calendar created as", filename
> print "Full path is", path
> """
> 
> Thanks, tutors,
> 
> Dick Moores
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list