[Tutor] SAVE FILE IN A SPECIFIC DIRECTORY

Peter Otten __peter__ at web.de
Tue Dec 7 00:10:31 CET 2010


Susana Iraiis Delgado Rodriguez wrote:

[UPPER CASE text is interpreted as shouting in emails and usenet posts. 
Please don't shout. Because spammers do it all the time it's more likely to 
make potential readers turn away from your writ rather than pay extra 
attention.]

> I'm trying to save files into a specific directory, the file will be
> generated from a python script. The script will look for some data stored
> in a directory which only allows to read files, it doesn't let the user
> write or modify information. But when I run the script I got the next
> error: Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit
> (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import crawler_shp
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "crawler_shp.py", line 103, in <module>
>     a = open (filepath +t,"w+")
> IOError: [Errno 2] No such file or directory:
> 'C\\Python26\\BUFFER1000_bd.txt'

Looks like you forgot a colon after the "C". Also note that C:\\Python26 is 
a bad place to store your own stuff.

A general remark on your code: it looks like you spend quite some time on 
it, and the up-front time it takes to make it reader-friendly will save you 
and others a multiple in debugging cost. So please

- Use 4-space indents
- Choose descriptive names, not a, t, f, that even if understood in the 
context of an expression will be forgotten three lines below. Also 'wrksht' 
or 'wrkSht' have no advantage over 'worksheet'. Vowels are your friend ;)
- Cut dead wood. Remove variables or imports that aren't currently used. You 
can always reintroduce them later on.
- Bonus: split your code into functions. It may seem like extra work at 
first, but it makes it easy to test small chunks of simple code and then to 
build more complex scripts by combining these "known good" pieces.

Peter



More information about the Tutor mailing list