2016-12-13 3 views
0
try: 
     print(filePath) 
     my_file = Path(filePath) 
    except ValueError: 
     print ValueError 

Der Versuch, diesen Code auszuführen hält einen Fehler werfen:Kann nicht Pfad (Datei) verwenden, ohne eine Ausnahme auszulösen

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 26: ordinal not in range(128)

Der Wert es Sprengung ist: Kellyanne Conway: Giuliani’s Withdrawal From Consideration A ‘Mutual Decision’ | MTP Daily | MSNBC.opus

Ich habe versucht, diese Zeichenfolge in Ascii und Unicode zu kodieren und es hat nicht funktioniert. Wie kann ich überprüfen, ob die Datei korrekt existiert? Danke.

+0

Und was ist 'Path'? – TigerhawkT3

+0

'von pathlib Import Path' – Antoine

+0

Python 2 oder Python 3? Es ist wichtig. Siehe http://stackoverflow.com/questions/33938079/why-python2-and-python3-treat-same-windows-directory-differently und https://pypi.python.org/pypi/pathlib/ (Suche nach "ASCII ") –

Antwort

0

Verwenden Sie os.path anstelle von pathlib.

import os 

print os.path.exists(filePath) 
print os.path.isfile(filePath) 

pathlib vor Python 3 behandelt nicht Nicht-ASCII-Zeichen gut. Siehe:

Why Python2 and Python3 treat same windows directory differently?

und

https://pypi.python.org/pypi/pathlib/ (Suche nach "ASCII")

+0

Funktioniert wie ein Charme, danke. Ich habe keine Upvotes mehr, werde aber abstimmen, wenn ich wieder etwas bekomme, thx – Antoine

+0

Ich habe versucht, Python 3 zu verwenden, aber es ist ein SSL-Fehler aufgetreten – Antoine

Verwandte Themen