6

I am trying to rename some files and I had a problem with some of them. I'll give an example: I tried to rename a file with the name "pronunciation_he_ אָדוֹם_אֲדוּמָה". It gave me an error and when I looked deeper into it, I figured out that it is because it can't open the file. The problem is not with the hebrew charaters but with the weird space. When I change the space to a normal space everything worked fine.

I am using python 2.7.15.

This is the code that changes the names of the files:

import os
for filename in os.listdir("E:\Project\Selenium\Check"):
    os.rename(os.path.join("E:\Project\Selenium\Check" ,filename), os.path.join("E:\Project\Selenium\Check" ,"check.mp3"))

The error: WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect

When I just tried opening the file:

import os
for filename in os.listdir("E:\Project\Selenium\Check"):
    file = open(os.path.join("E:\Project\Selenium\Check" ,filename))

The error: IOError: [Errno 22] invalid mode ('r') or filename: 'E:\Project\Selenium\Check\pronunciation_he_?\xe0\xc8\xe3\xe5\xc9\xed_\xe0\xc2\xe3\xe5\xcc\xee\xc8\xe4.mp3'

Thanks in advance!

EDIT CHANGING TO PYTHON 3 JUST SOLVED THE PROBLEM

Netanel
  • 477
  • 2
  • 11
  • you can use the 8.3 file name of the file (it's "short name"). and there is already an answer on how to retrieve the short file name on windows: https://stackoverflow.com/questions/23598289/how-to-get-windows-short-file-name-in-python – AntiMatterDynamite Oct 31 '18 at 10:23
  • try changing the path to `E:/Project/Selenium/Check` or `E:\\Project\\Selenium\\Check` – arunkumar Oct 31 '18 at 10:25
  • 1
    use raw strings, as else they're interpreted: `r"E:\Project\Selenium\Check"`, but in python 2 with uppercase it's not the issue – Jean-François Fabre Oct 31 '18 at 10:29
  • @arunkumar From what I saw the problem is with the filename and not the path – Netanel Oct 31 '18 at 10:33
  • @Jean-FrançoisFabre I could open other files in this directory, I think the problem is with the filename. – Netanel Oct 31 '18 at 10:34
  • @AntiMatterDynamite With the win32api library it gave me the same error: pywintypes.error: (123, 'GetShortPathName', 'The filename, directory name, or volume label syntax is incorrect.') So I tried to get it with the code in the answer and it returned me an empty string. – Netanel Oct 31 '18 at 10:44
  • I have an idea: can you try "extended" file path?: `r"\\?\E:\Project\Selenium\Check"` – Jean-François Fabre Oct 31 '18 at 12:00

0 Answers0