audiolab
audiolab copied to clipboard
python scikits.audiolab Sndfile special chars in file name
Hi , I am trying to get a wav file information but Sndfile is unable to read file with special characters in path and file name(查找問題daw.wav) , i am unable to get information in any way i mentioned in the code , i tried passing file path to Sndfile with diferent encoding to but didnt work , but if i pass this 'C:\Users\Furqan\Desktop\test\DAW\1.wav' it works fine , Help me ! Thanks is advance
My Code is
-- coding: UTF-8 --
from scikits.audiolab import Sndfile
from os import walk, path, stat
track1 = r'C:\Users\Furqan\Desktop\test\查找問題daw\1.wav'
#track1 = r'C:\Users\Furqan\Desktop\test\DAW\1.wav'
try:
track_one_file_obj = Sndfile(track1, 'r')
except:
print('Simple didnt work')
print(Exception.message)
print('')
pass
try:
track_one_file_obj = Sndfile(track1.decode('cp1252'), 'r')
except:
print('cp1252 didnt work')
print(Exception.message)
print('')
pass
try:
track_one_file_obj = Sndfile(track1.encode('utf-8'), 'r')
print(track_one_file_obj)
except:
print('encode didnt work')
print(Exception.message)
print('')
pass
try:
track_one_file_obj = Sndfile(track1.encode('utf8'), 'r')
print(track_one_file_obj)
except:
print('encode didnt work')
print(Exception.message)
print('')
pass
try:
track_one_file_obj = Sndfile(track1.decode('utf-8'), 'r')
print(track_one_file_obj)
except:
print('decode didnt work')
print(Exception.message)
print('')
pass
try:
track_one_file_obj = Sndfile(track1.decode('utf8'), 'r')
print(track_one_file_obj)
except:
print('decode didnt work')
print(Exception.message)
print('')
pass
print(track_one_file_obj.nframes)
If you don't want to wait for scikits.audiolab to fix this, you can use PySoundFile, which supports those kinds of file names since version 0.8.0.
If such characters are supported by you locale code page, what about:
For Python 3: https://docs.python.org/3/howto/unicode.html
#!/usr/bin/env python3
from scikits.audiolab import Sndfile
track1 = r'C:\Users\Furqan\Desktop\test\查找問題daw\1.wav'
sndfile = Sndfile(track1.encode(sys.filesystemencoding()), 'r')
For Python 2: https://docs.python.org/2/howto/unicode.html
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from scikits.audiolab import Sndfile
track1 = ru'C:\Users\Furqan\Desktop\test\查找問題daw\1.wav'
sndfile = Sndfile(track1.encode(sys.filesystemencoding()), 'r')
It should work whenever the locale codepage is able to encode the unicode characters in the filename. But that would be a limitation on what the system is able to do. using the unicode string (or decode it as unicode if it is bytes provided that we know the original encoding) and pass it to the wchar_t function should be the way in windows.