Changing filenames from Greeklish => Greek (subprocess complain)
Νικόλαος Κούρας
nikos.gr33k at gmail.com
Sun Jun 9 04:11:40 EDT 2013
I'm sorry posted by mistake unnessary code: here is the correct one that prodiuced the above error:
#========================================================
# Collect directory and its filenames as bytes
path = b'/home/nikos/public_html/data/apps/'
files = os.listdir( path )
for filename in files:
# Compute 'path/to/filename'
filepath_bytes = path + filename
for encoding in ('utf-8', 'iso-8859-7', 'latin-1'):
try:
filepath = filepath_bytes.decode( encoding )
except UnicodeDecodeError:
continue
# Rename to something valid in UTF-8
if encoding != 'utf-8':
os.rename( filepath_bytes, filepath.encode('utf-8') )
assert os.path.exists( filepath )
break
else:
# This only runs if we never reached the break
raise ValueError( 'unable to clean filename %r' % filepath_bytes )
#========================================================
# Collect filenames of the path dir as strings
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()
if not data:
# First time for file; primary key is automatic, hit is defaulted
print( "iam here", filename + '\n' )
cur.execute('''INSERT INTO files (url, host, lastvisit) VALUES (%s, %s, %s)''', (filename, host, lastvisit) )
except pymysql.ProgrammingError as e:
print( repr(e) )
#========================================================
# Collect filenames of the path dir as strings
filenames = os.listdir( '/home/nikos/public_html/data/apps/' )
filepaths = set()
# Build a set of 'path/to/filename' based on the objects of path dir
for filename in filenames:
filepaths.add( filename )
# Delete spurious
cur.execute('''SELECT url FROM files''')
data = cur.fetchall()
# Check database's filenames against path's filenames
for rec in data:
if rec not in filepaths:
cur.execute('''DELETE FROM files WHERE url = %s''', rec )
More information about the Python-list
mailing list