Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'output': 0.05; 'encoded': 0.07; 'rename': 0.07; 'utf-8': 0.07; 'filename': 0.09; 'filenames': 0.09; 'happen,': 0.09; 'root,': 0.09; 'try:': 0.09; "'''": 0.16; '8bit%:26': 0.16; 'encodings': 0.16; 'filename)': 0.16; 'files:': 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; 'renaming': 0.16; 'sys.exit(0)': 0.16; 'url:py': 0.16; 'files.': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'appears': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'switch': 0.26; 'this:': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'skip:- 40': 0.29; 'mode': 0.30; 'code': 0.31; 'ahead.': 0.31; "skip:' 40": 0.31; 'skip:b 30': 0.33; 'subject:from': 0.34; 'subject: (': 0.35; 'except': 0.35; 'test': 0.35; 'but': 0.35; 'skip:f 40': 0.36; 'turn': 0.37; 'skip:- 20': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; "you're": 0.61; 'first': 0.61; 'here:': 0.62; 'name': 0.63; 'skip:n 10': 0.64; 'url:cgi-bin': 0.65; 'dont': 0.67; 'header:Reply-To:1': 0.67; 'helping': 0.70; 'reply-to:no real name:2**0': 0.71; '8bit%:100': 0.72; 'walk': 0.74; '--->': 0.84; 'reply-to:addr:python.org': 0.84; '8bit%:67': 0.93 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=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=uw23S90zXSUA:10 a=NXsfvHnzAAAA:8 a=IT6m1vurX46KKkUO7XwA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Thu, 06 Jun 2013 13:50:52 +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> In-Reply-To: <808b7897-8ed4-4e0e-8976-4a22ae7f24cd@googlegroups.com> Content-Type: text/plain; charset=UTF-8; format=flowed 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370523054 news.xs4all.nl 15913 [2001:888:2000:d::a6]:60322 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47226 On 06/06/2013 13:04, Νικόλαος Κούρας wrote: > First of all thank you for helping me MRAB. > After make some alternation to your code ia have this: > > ---------------------------------------- > # Give the path as a bytestring so that we'll get the filenames as bytestrings > path = b"/home/nikos/public_html/data/apps/" > > # Setting TESTING to True will make it print out what renamings it will do, but not actually do them > TESTING = True > > # Walk through the files. > for root, dirs, files in os.walk( path ): > for filename in files: > 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 the filenames are encoded in ISO-8859-7, so decode from that and re-encode to UTF-8 > new_filename = filename.decode('iso-8859-7').encode('utf-8') > > old_path = os.path.join(root, filename) > new_path = os.path.join(root, new_filename) > if TESTING: > print( '''
Will rename {!r} ---> {!r}

'''.format( old_path, new_path ) ) > else: > print( '''
Renaming {!r} ---> {!r}

'''.format( old_path, new_path ) ) > os.rename( old_path, new_path ) > sys.exit(0) > ------------------------- > > and the output can be seen here: http://superhost.gr/cgi-bin/files.py > > We are in test mode so i dont know if when renaming actually take place what the encodings will be. > > Shall i switch off test mode and try it for real? > The first one is '/home/nikos/public_html/data/apps/Ευχή του Ιησού.mp3'. The second one is '/home/nikos/public_html/data/apps/Σκέψου έναν αριθμό.exe'. These names are currently encoded in ISO-8859-7, but will be encoded in UTF-8 if you turn off test mode. If you're happy for that change to happen, then go ahead.