Has some errors:

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

# iterate over all filenames in the apps directory
for filename in path:
        # Grabbing just the filename from path
        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 bytestreams --> utf-8 bytestreams
                old_path = b'/home/nikos/public_html/data/apps/' + b'filename')
                new_path = b'/home/nikos/public_html/data/apps/' + 
b'new_filename')
                os.rename( old_path, new_path )


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

# Load'em
for filename 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''', 
(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) )


#========================================================
path = 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 path
        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,) 
)
-------------------------------

The only problem now is the bytestrings:

ni...@superhost.gr [~/www/cgi-bin]# [Thu Jun 06 23:50:42 2013] [error] [client 
79.103.41.173]   File "files.py", line 78
[Thu Jun 06 23:50:42 2013] [error] [client 79.103.41.173]     old_path = 
b'/home/nikos/public_html/data/apps/' + b'filename')
[Thu Jun 06 23:50:42 2013] [error] [client 79.103.41.173]                       
                                            ^
[Thu Jun 06 23:50:42 2013] [error] [client 79.103.41.173] SyntaxError: invalid 
syntax


Dont know how to add a bytestremed path to a bytestream filename
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to