Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'charset:iso-8859-7': 0.04; 'syntax': 0.04; 'insert': 0.05; 'mrab': 0.05; 'skip:\xa0 30': 0.05; 'encoded': 0.07; 'rename': 0.07; 'utf-8': 0.07; 'string': 0.09; 'data:': 0.09; 'filename': 0.09; 'filenames': 0.09; 'happen,': 0.09; 'host,': 0.09; 'iterate': 0.09; 'root,': 0.09; 'skip:# 60': 0.09; 'try:': 0.09; '%s,': 0.16; 'compute': 0.16; 'do!': 0.16; 'doesnt': 0.16; 'exactly,': 0.16; 'file;': 0.16; 'filename)': 0.16; 'files:': 0.16; 'fixing.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'new_path': 0.16; 'path.': 0.16; 'path:': 0.16; 'syntaxerror:': 0.16; 'apps': 0.16; 'files.': 0.16; 'wrote:': 0.18; 'replacing': 0.19; "skip:' 30": 0.19; 'appears': 0.22; 'example': 0.22; 'header :User-Agent:1': 0.23; 'error': 0.23; 'directory.': 0.24; 'unicode': 0.24; '>': 0.26; 'primary': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'mix': 0.30; 'code': 0.31; 'ahead.': 0.31; 'names.': 0.31; 'skip:7 10': 0.31; 'skip:= 40': 0.31; 'unique,': 0.31; 'file': 0.32; 'checking': 0.33; 'skip:b 30': 0.33; 'subject:from': 0.34; 'subject: (': 0.35; "can't": 0.35; 'except': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'skip:f 40': 0.36; 'doing': 0.36; 'should': 0.36; 'changing': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'skip:u 10': 0.60; 'full': 0.61; "you're": 0.61; 'first': 0.61; 'name': 0.63; 'skip:n 10': 0.64; 'here': 0.66; 'dont': 0.67; 'header:Reply-To:1': 0.67; 'invalid': 0.68; 'reply-to:no real name:2**0': 0.71; '8bit%:100': 0.72; 'walk': 0.74; 'grabbing': 0.84; 'greek': 0.84; 'reply-to:addr:python.org': 0.84; '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0': 0.84; '2013': 0.98 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=KrN0hwmN c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=oyR3mlnJdzkA:10 a=AdgiQdVXbpoA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=E_FQspuQcigA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=uw23S90zXSUA:10 a=4RBUngkUAAAA:8 a=9MBlabfybEJb5u9yYJ8A:9 a=Ube0z8Se5fDpiw8j:21 a=o3X3QV6JOaEA:10 a=_W_S_7VecoQA:10 a=Bm6qEjDGwGEA:10 X-AUTH: mrabarnett:2500 Date: Thu, 06 Jun 2013 20:42:25 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Changing filenames from Greeklish => Greek (subprocess complain) References: <2c425f2b-99de-4453-964e-c585f2043f71@googlegroups.com> <306a22ea-fbf7-4097-af31-121a999957d6@googlegroups.com> <9c482ba0-23ac-4e66-a0e1-a18be9fd82d8@googlegroup> <06a19483-65df-4fcd-9430-b45a01c9dbab@googlegroups.com> <51aed313$0$11118$c3e8da3@news.astraweb.com> <4c19b71d-4de5-41ad-b6ae-fb133a6c331e@googlegroups.com> <2be143c4-77c6-4c84-ba1c-46b02bd503ff@googlegroups.com> <1465c96b-c33e-4d5b-894e-b184c031a185@googlegroups.com> <808b7897-8ed4-4e0e-8976-4a22ae7f24cd@googlegroups.com> <332812d7-71b3-4bbb-a846-09827a6df65d@googlegroups.com> In-Reply-To: <332812d7-71b3-4bbb-a846-09827a6df65d@googlegroups.com> Content-Type: text/html; charset=ISO-8859-7 Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 130 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370547736 news.xs4all.nl 16004 [2001:888:2000:d::a6]:32984 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47266
On 06/06/2013 19:13, Νικόλαος Κούρας wrote:
Τη Πέμπτη, 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/' )
os.listdir returns a list of the names of the objects in the given directory.

# iterate over all filenames in the apps directory
Exactly, all the names.

for fullpath in path
	# Grabbing just the filename from path
The name is a bytestring. Note, name, NOT full path.

The following line will fail because the name is a bytestring, and you can't mix bytestrings with Unicode strings:
	filename = fullpath.replace( '/home/nikos/public_html/data/apps/', '' )
                                           ^ bytestring                ^ Unicode string                                                 ^ Unicode string
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?
You're changing things without checking what they do!