<html>
<head>
<meta content="text/html; charset=ISO-8859-7"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 06/06/2013 19:13, Νικόλαος Κούρας
wrote:<br>
</div>
<blockquote
cite="mid:332812d7-71b3-4bbb-a846-09827a6df65d@googlegroups.com"
type="cite">
<pre wrap="">Τη Πέμπτη, 6 Ιουνίου 2013 3:50:52 μ.μ. UTC+3, ο χρήστης MRAB έγραψε:
> If you're happy for that change to happen, then go ahead.
I have made some modifications to the code you provided me but i think something that doesnt accur to me needs fixing.
for example i switched:
# Give the path as a bytestring so that we'll get the filenames as bytestrings
path = b"/home/nikos/public_html/data/apps/"
# Walk through the files.
for root, dirs, files in os.walk( path ):
for filename in files:
to:
# Give the path as a bytestring so that we'll get the filenames as bytestrings
path = os.listdir( b'/home/nikos/public_html/data/apps/' )
</pre>
</blockquote>
os.listdir returns a list of the names of the objects in the given
directory.<br>
<br>
<blockquote
cite="mid:332812d7-71b3-4bbb-a846-09827a6df65d@googlegroups.com"
type="cite">
<pre wrap=""># iterate over all filenames in the apps directory
</pre>
</blockquote>
Exactly, all the <u>names</u>.<br>
<br>
<blockquote
cite="mid:332812d7-71b3-4bbb-a846-09827a6df65d@googlegroups.com"
type="cite">
<pre wrap="">for fullpath in path
# Grabbing just the filename from path
</pre>
</blockquote>
The name is a <u>bytestring</u>. Note, <u>name</u>, NOT full path.<br>
<br>
The following line will fail because the name is a <u>bytestring</u>,
and you can't mix bytestrings with Unicode strings:<br>
<blockquote
cite="mid:332812d7-71b3-4bbb-a846-09827a6df65d@googlegroups.com"
type="cite">
<pre wrap=""> filename = fullpath.replace( '/home/nikos/public_html/data/apps/', '' )</pre>
</blockquote>
^ bytestring
^ Unicode string
^ Unicode string<br>
<blockquote
cite="mid:332812d7-71b3-4bbb-a846-09827a6df65d@googlegroups.com"
type="cite">
<pre wrap="">I dont know if it has the same effect:
Here is the the whole snippet:
=============================================
# Give the path as a bytestring so that we'll get the filenames as bytestrings
path = os.listdir( b'/home/nikos/public_html/data/apps/' )
# iterate over all filenames in the apps directory
for fullpath in path
# Grabbing just the filename from path
filename = fullpath.replace( '/home/nikos/public_html/data/apps/', '' )
try:
# Is this name encoded in utf-8?
filename.decode('utf-8')
except UnicodeDecodeError:
# Decoding from UTF-8 failed, which means that the name is not valid utf-8
# It appears that this filename is encoded in greek-iso, so decode from that and re-encode to utf-8
new_filename = filename.decode('iso-8859-7').encode('utf-8')
# rename filename form greek bytestream-> utf-8 bytestream
old_path = os.path.join(root, filename)
new_path = os.path.join(root, new_filename)
os.rename( old_path, new_path )
#============================================================
# Compute a set of current fullpaths
path = os.listdir( '/home/nikos/public_html/data/apps/' )
# Load'em
for fullpath in path:
try:
# Check the presence of a file against the database and insert if it doesn't exist
cur.execute('''SELECT url FROM files WHERE url = %s''', (fullpath,) )
data = cur.fetchone() #URL is unique, so should only be one
if not data:
# First time for file; primary key is automatic, hit is defaulted
cur.execute('''INSERT INTO files (url, host, lastvisit) VALUES (%s, %s, %s)''', (fullpath, host, lastvisit) )
except pymysql.ProgrammingError as e:
print( repr(e) )
==================================================================
The error is:
[Thu Jun 06 21:10:23 2013] [error] [client 79.103.41.173] File "files.py", line 64
[Thu Jun 06 21:10:23 2013] [error] [client 79.103.41.173] for fullpath in path
[Thu Jun 06 21:10:23 2013] [error] [client 79.103.41.173] ^
[Thu Jun 06 21:10:23 2013] [error] [client 79.103.41.173] SyntaxError: invalid syntax
Doesn't os.listdir( ...) returns a list with all filenames?
But then again when replacing take place to shert the fullpath to just the filane i think it doesn't not work because the os.listdir was opened as bytestring and not as a string....
What am i doing wrong?
</pre>
</blockquote>
You're changing things without checking what they do!<br>
<br>
</body>
</html>