<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 7/6/2013 10:42 πμ, Michael Weylandt
      wrote:<br>
    </div>
    <blockquote
      cite="mid:5FFE659B-8271-4AF7-9116-96B763972F95@gmail.com"
      type="cite"><br>
      <pre wrap="">os.rename( filepath_bytes filepath.encode('utf-8') 
</pre>
      <pre wrap="">
Missing comma, which is, after all, just a matter of syntax so it can't matter, right?

</pre>
    </blockquote>
    I doubted that os.rename arguments must be comma seperated.<br>
    But ater reading the docs.<br>
    <br>
    <dt id="os.rename" style="color: rgb(0, 0, 0); font-family:
      sans-serif; font-size: 16px; font-style: normal; font-variant:
      normal; font-weight: normal; letter-spacing: normal; line-height:
      normal; orphans: auto; text-align: start; text-indent: 0px;
      text-transform: none; white-space: normal; widows: auto;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(255, 255, 255);"><tt class="descclassname"
        style="background-color: transparent; padding: 0px 1px;
        font-size: 0.95em;">s.</tt><tt class="descname"
        style="background-color: transparent; padding: 0px 1px;
        font-size: 1.2em; font-weight: bold;">rename</tt><big>(</big><em>src</em>,<span
        class="Apple-converted-space"> </span><em>dst</em><big>)</big><a
        class="headerlink"
        href="http://docs.python.org/2/library/os.html#os.rename"
        title="Permalink to this definition" style="color: rgb(198, 15,
        15); text-decoration: none; visibility: hidden; font-size:
        0.8em; padding: 0px 4px;"></a></dt>
    <dd style="margin-top: 3px; margin-bottom: 10px; margin-left: 30px;
      text-align: justify; line-height: 20px; color: rgb(0, 0, 0);
      font-family: sans-serif; font-size: 16px; font-style: normal;
      font-variant: normal; font-weight: normal; letter-spacing: normal;
      orphans: auto; text-indent: 0px; text-transform: none;
      white-space: normal; widows: auto; word-spacing: 0px;
      -webkit-text-stroke-width: 0px; background-color: rgb(255, 255,
      255);">
      <p style="margin-top: 0px; text-align: justify; line-height:
        20px;">Rename the file or directory<span
          class="Apple-converted-space"> </span><em>src</em><span
          class="Apple-converted-space"> </span>to<span
          class="Apple-converted-space"> </span><em>dst</em>. If<span
          class="Apple-converted-space"> </span><em>dst</em><span
          class="Apple-converted-space"> </span>is a directory,<span
          class="Apple-converted-space"> </span><a class="reference
          internal"
href="http://docs.python.org/2/library/exceptions.html#exceptions.OSError"
          title="exceptions.OSError" style="color: rgb(53, 95, 124);
          text-decoration: none;"><tt class="xref py py-exc docutils
            literal" style="background-color: transparent; padding: 0px
            1px; font-size: 0.95em; font-weight: bold;"><span
              class="pre">OSError</span></tt></a><span
          class="Apple-converted-space"> </span>will be raised. On Unix,
        if<span class="Apple-converted-space"> </span><em>dst</em><span
          class="Apple-converted-space"> </span>exists and is a file, it
        will be replaced silently if the user has permission. The
        operation may fail on some Unix flavors if<span
          class="Apple-converted-space"> </span><em>src</em><span
          class="Apple-converted-space"> </span>and<span
          class="Apple-converted-space"> </span><em>dst</em><span
          class="Apple-converted-space"> </span>are on different
        filesystems. If successful, the renaming will be an atomic
        operation (this is a POSIX requirement). On Windows, if<span
          class="Apple-converted-space"> </span><em>dst</em><span
          class="Apple-converted-space"> </span>already exists,<span
          class="Apple-converted-space"> </span><a class="reference
          internal"
href="http://docs.python.org/2/library/exceptions.html#exceptions.OSError"
          title="exceptions.OSError" style="color: rgb(53, 95, 124);
          text-decoration: none;"><tt class="xref py py-exc docutils
            literal" style="background-color: transparent; padding: 0px
            1px; font-size: 0.95em; font-weight: bold;"><span
              class="pre">OSError</span></tt></a><span
          class="Apple-converted-space"> </span>will be raised even if
        it is a file; there may be no way to implement an atomic rename
        when<span class="Apple-converted-space"> </span><em>dst</em><span
          class="Apple-converted-space"> </span>names an existing file.</p>
      <p style="margin-top: 0px; text-align: justify; line-height:
        20px;">Availability: Unix, Windows.</p>
    </dd>
    Indeed it has to be:<br>
    <pre wrap="">os.rename( filepath_bytes, filepath.encode('utf-8') 

'mv source target' didn't require commas so i though it was safe to assume that os.rename did not either.


I'am happy to announce that after correcting many idiotic error like commas, missing colons and declaring of variables, this surrogate erro si the last i get.
I still dont understand what surrogate means. In english means replacement.
Here is the code:


#========================================================
# Collect filenames of the path dir as bytes
filename_bytes = os.listdir( b'/home/nikos/public_html/data/apps/' )

# Iterate over all filenames in the path dir
for filename in filename_bytes:
        # Compute 'path/to/filename' in bytes
        filepath_bytes = b'/home/nikos/public_html/data/apps/' + b'filename'
        try:
                filepath = filepath_bytes.decode('utf-8')
        except UnicodeDecodeError:
                try:
                        filepath = filepath_bytes.decode('iso-8859-7')
                        
                        # Rename current filename from greek bytes => utf-8 bytes
                        os.rename( filepath_bytes, filepath.encode('utf-8') )
                except UnicodeDecodeError:
                        print( '''I give up! This filename is unreadable! ''')


#========================================================
# Get filenames of the apps directory as unicode
filenames = os.listdir( '/home/nikos/public_html/data/apps/' )

# Load'em
for filename in filenames:
        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''', (filename,) )
                data = cur.fetchone()        #filename 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)''', (filename, host, lastvisit) )
        except pymysql.ProgrammingError as e:
                print( repr(e) )


#========================================================
filenames = os.listdir( '/home/nikos/public_html/data/apps/' )
filenames = ()

# Build a set of 'path/to/filename' based on the objects of path dir
for filename in filenames:
        filenames.add( filename )

# Delete spurious 
cur.execute('''SELECT url FROM files''')
data = cur.fetchall()

# Check database's filenames against path's filenames
for filename in data:
        if filename not in filenames:
                cur.execute('''DELETE FROM files WHERE url = %s''', (filename,) )



=================================

[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173]   File "/home/nikos/public_html/cgi-bin/files.py", line 88, in <module>
[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173]     cur.execute('''SELECT url FROM files WHERE url = %s''', filename )
[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173]   File "/usr/local/lib/python3.3/site-packages/PyMySQL3-0.5-py3.3.egg/pymysql/cursors.py", line 108, in execute
[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173]     query = query.encode(charset)
[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173] UnicodeEncodeError: 'utf-8' codec can't encode character '\\udcce' in position 35: surrogates not allowed

</pre>
    <br>
    <br>
    <div class="moz-signature">-- <br>
      <a href="http://superhost.gr"><font color="blue">Webhost</font></a><font
        color="blue">
        <font color="lime"> &&
          <a href="http://psariastonafro.wordpress.com"><font
              color="red">Weblog</font></a></font></font></div>
  </body>
</html>