Re: [Tutor] Save file in a specific directory

2010-12-09 Thread Susana Iraiis Delgado Rodriguez
Thank you Jerry! The suggestion you told me make my code worked!

>
>
>
> dbf = Dbf(d,new=False, readOnly=True)
>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Save file in a specific directory

2010-12-07 Thread Jerry Hill
> When I run the script I got the next error:
 import crawler_shp
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "crawler_shp.py", line 105, in 
>     dbf = Dbf(d,new=False)
>   File "C:\Python26\lib\site-packages\dbf.py", line 125, in __init__
>     self.stream = file(f, ("r+b", "rb")[bool(readOnly)])
> IOError: [Errno 13] Permission denied: 'R:\\Aplicaciones\\IRISv3\\mis
> proyectos\
> \HURACAN\\BUFFER1000.dbf'
>
> The error is pointing to a library I used to make the script run: dbf.py. in
> thid lines:
> if isinstance(f, basestring):
>     # a filename
>     self.name = f
>     if new:
>     # new table (table file must be
>     # created or opened and truncated)
>     self.stream = file(f, "w+b")
>     else:
>     # tabe file must exist
>     self.stream = file(f, ("r+b", "rb")[bool(readOnly)])


Sounds like you need to change your dbf object to something like this:
 dbf = Dbf(d,new=False, readOnly=True)

Note that I don't know anything about the Dbf module, other than what
you just posted.  If you really have read permission, but not write
permissions, this should do what you want though.

-- 
Jerry
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Save file in a specific directory

2010-12-07 Thread Joel Goldstick
On Tue, Dec 7, 2010 at 12:20 PM, Susana Iraiis Delgado Rodriguez <
susana.delgad...@utzmg.edu.mx> wrote:

> My other message was incomplete, it was a mistake: This is the correct one
>
> 2010/12/7 Susana Iraiis Delgado Rodriguez 
>
>  I make a script to redirect a txt file from an external directory, but in
>> this directory I don't have permission to write, just to read data. So I
>> make this module:
>> import os, time,fnmatch
>> from xlwt import Workbook
>> from osgeo import ogr,gdal,osr
>> from dbf import *
>> gdal.AllRegister()
>> file_list = []
>> folders = None
>> for root, folders, files in os.walk( "R:\\" ):
>> for filename in fnmatch.filter(files, '*.shp'):
>> file_list.append(os.path.join(root, filename))
>> wrkbk = Workbook()
>> wksht = wrkbk.add_sheet('shp')
>> wksht.row(0).write(0,'ruta')
>> wksht.row(0).write(1,'archivo')
>> wksht.row(0).write(2,'estructura bd')
>> for row, filepath in enumerate(file_list, start=1):
>> wksht.row(row).write(0, filepath)
>> (ruta, filename) = os.path.split(filepath)
>>  wksht.row(row).write(1, filename)
>>  f = os.path.splitext(filename)
>>  t = f[0]+'_bd.txt'
>>  d = n[0]+'.dbf'
>>  if os.path.lexists(d):
>>filepath = "C:\\Python26\\"
>>a = open (filepath +t,"w+")
>>dbf = Dbf(d,new=False)
>>for fldName in dbf.fieldDefs:
>>a.write(fldName.name)
>>a.write(" || ")
>>a.write(fldName.typeCode)
>>a.write("\n")
>>dbf.close()
>>a.close()
>>wksht.row(row).write(2, t)
>>   else:
>>   print "El archivo " +n[0]+".shp" " no tiene dbf"
>>   wksht.row(row).write(10, "Sin bd"
>> wrkbk.save('C:\\Python26\\biblio_shp.xls')
>>
> When I run the script I got the next error:
> >>> import crawler_shp
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "crawler_shp.py", line 105, in 
>
> dbf = Dbf(d,new=False)
>   File "C:\Python26\lib\site-packages\dbf.py", line 125, in __init__
> self.stream = file(f, ("r+b", "rb")[bool(readOnly)])
> IOError: [Errno 13] Permission denied: 'R:\\Aplicaciones\\IRISv3\\mis
> proyectos\
> \HURACAN\\BUFFER1000.dbf'
>
> The error is pointing to a library I used to make the script run: dbf.py.
> in thid lines:
> if isinstance(f, basestring):
> # a filename
> self.name = f
> if new:
> # new table (table file must be
> # created or opened and truncated)
> self.stream = file(f, "w+b")
> else:
> # tabe file must exist
> self.stream = file(f, ("r+b", "rb")[bool(readOnly)])
>
>
>>
>>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
Do you have write privilege in the directory where you want to create the
file?

-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Save file in a specific directory

2010-12-07 Thread Susana Iraiis Delgado Rodriguez
My other message was incomplete, it was a mistake: This is the correct one

2010/12/7 Susana Iraiis Delgado Rodriguez 

> I make a script to redirect a txt file from an external directory, but in
> this directory I don't have permission to write, just to read data. So I
> make this module:
> import os, time,fnmatch
> from xlwt import Workbook
> from osgeo import ogr,gdal,osr
> from dbf import *
> gdal.AllRegister()
> file_list = []
> folders = None
> for root, folders, files in os.walk( "R:\\" ):
> for filename in fnmatch.filter(files, '*.shp'):
> file_list.append(os.path.join(root, filename))
> wrkbk = Workbook()
> wksht = wrkbk.add_sheet('shp')
> wksht.row(0).write(0,'ruta')
> wksht.row(0).write(1,'archivo')
> wksht.row(0).write(2,'estructura bd')
> for row, filepath in enumerate(file_list, start=1):
> wksht.row(row).write(0, filepath)
> (ruta, filename) = os.path.split(filepath)
>  wksht.row(row).write(1, filename)
>  f = os.path.splitext(filename)
>  t = f[0]+'_bd.txt'
>  d = n[0]+'.dbf'
>  if os.path.lexists(d):
>filepath = "C:\\Python26\\"
>a = open (filepath +t,"w+")
>dbf = Dbf(d,new=False)
>for fldName in dbf.fieldDefs:
>a.write(fldName.name)
>a.write(" || ")
>a.write(fldName.typeCode)
>a.write("\n")
>dbf.close()
>a.close()
>wksht.row(row).write(2, t)
>   else:
>   print "El archivo " +n[0]+".shp" " no tiene dbf"
>   wksht.row(row).write(10, "Sin bd"
> wrkbk.save('C:\\Python26\\biblio_shp.xls')
>
When I run the script I got the next error:
>>> import crawler_shp
Traceback (most recent call last):
  File "", line 1, in 
  File "crawler_shp.py", line 105, in 
dbf = Dbf(d,new=False)
  File "C:\Python26\lib\site-packages\dbf.py", line 125, in __init__
self.stream = file(f, ("r+b", "rb")[bool(readOnly)])
IOError: [Errno 13] Permission denied: 'R:\\Aplicaciones\\IRISv3\\mis
proyectos\
\HURACAN\\BUFFER1000.dbf'

The error is pointing to a library I used to make the script run: dbf.py. in
thid lines:
if isinstance(f, basestring):
# a filename
self.name = f
if new:
# new table (table file must be
# created or opened and truncated)
self.stream = file(f, "w+b")
else:
# tabe file must exist
self.stream = file(f, ("r+b", "rb")[bool(readOnly)])


>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Save file in a specific directory

2010-12-07 Thread Susana Iraiis Delgado Rodriguez
I make a script to redirect a txt file from an external directory, but in
this directory I don't have permission to write, just to read data. So I
make this module:
import os, time,fnmatch
from xlwt import Workbook
from osgeo import ogr,gdal,osr
from dbf import *
gdal.AllRegister()
file_list = []
folders = None
for root, folders, files in os.walk( "R:\\" ):
for filename in fnmatch.filter(files, '*.shp'):
file_list.append(os.path.join(root, filename))
wrkbk = Workbook()
wksht = wrkbk.add_sheet('shp')
wksht.row(0).write(0,'ruta')
wksht.row(0).write(1,'archivo')
wksht.row(0).write(2,'estructura bd')
for row, filepath in enumerate(file_list, start=1):
wksht.row(row).write(0, filepath)
(ruta, filename) = os.path.split(filepath)
 wksht.row(row).write(1, filename)
 f = os.path.splitext(filename)
 t = f[0]+'_bd.txt'
 d = n[0]+'.dbf'
 if os.path.lexists(d):
   filepath = "C:\\Python26\\"
   a = open (filepath +t,"w+")
   dbf = Dbf(d,new=False)
   for fldName in dbf.fieldDefs:
a.write(fldName.name)
a.write(" || ")
a.write(fldName.typeCode)
a.write("\n")
   dbf.close()
   a.close()
wksht.row(row).write(2, t)
   else:
   print "El archivo " +n[0]+".shp" " no tiene dbf"
   wksht.row(row).write(10, "Sin bd")


wrkbk.save('C\\Python26\\biblio_shp.xls')
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SAVE FILE IN A SPECIFIC DIRECTORY

2010-12-06 Thread Peter Otten
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 "", line 1, in 
>   File "crawler_shp.py", line 103, in 
> 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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] SAVE FILE IN A SPECIFIC DIRECTORY

2010-12-06 Thread Susana Iraiis Delgado Rodriguez
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 "", line 1, in 
  File "crawler_shp.py", line 103, in 
a = open (filepath +t,"w+")
IOError: [Errno 2] No such file or directory:
'C\\Python26\\BUFFER1000_bd.txt'
>>>
My script is:
import os, time, socket, pylab,fnmatch
from xlwt import Workbook
from osgeo import ogr,gdal,osr
from dbf import *

gdal.AllRegister()
file_list = []
folders = None
for root, folders, files in os.walk( "R:\\" ):
 for filename in fnmatch.filter(files, '*.shp'):
  file_list.append(os.path.join(root, filename))
wrkbk = Workbook()
wksht = wrkbk.add_sheet('shp')
wksht.row(0).write(0,'ruta')
wksht.row(0).write(1,'archivo')
wksht.row(0).write(2,'estructura bd')
for row, filepath in enumerate(file_list, start=1):
 wksht.row(row).write(0, filepath)
 (ruta, filename) = os.path.split(filepath)
 wksht.row(row).write(1, filename)
  f = os.path.splitext(filename)
  t = f[0]+'_bd.txt'
  d = n[0]+'.dbf'
  if os.path.lexists(d):
  filepath = "C\\Python26\\"
   a = open (filepath +t,"w+")
   dbf = Dbf(d,new=False)


   for fldName in dbf.fieldDefs:
a.write(fldName.name)
a.write(" || ")
a.write(fldName.typeCode)
a.write("\n")
   dbf.close()
   a.close()
wksht.row(row).write(2, t)
   else:
   print "El archivo " +n[0]+".shp" " no tiene dbf"
   wksht.row(row).write(10, "Sin bd")


wrkbk.save('C\\Python26\\biblio_shp.xls')
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor