Solution for Python Version 2.7 os.path.isdir Chinese Path Return to false

  • 2021-06-29 11:26:58
  • OfStack

Question Background:

I wanted to write a script to process the files on the hard disk and sort them, but I found a problem. There was a problem using the python built-in os module. See the example for details.

Primary method used (python version 2.7)

Example:

Create two folders under the computer's D drive and two files and directories under each folder:

a: D:\\test\\test.txt

b: D:\TestTest.txt

Several methods to test os.path using a and b paths respectively


#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import os
a = "D:\\test\\test.txt"
b = "D:\ test \ test .txt"
 
print "a Does the file exist? " + str(os.path.exists(a))
print "a Is the path absolute? " + str(os.path.isabs(a))
print "a Is it a directory? " + str(os.path.isdir(a))
print "a Is it a file? " + str(os.path.isfile(a))
print "================================="
print "b Does the file exist? " + str(os.path.exists(b))
print "b Is the path absolute? " + str(os.path.isabs(b))
print "b Is it a directory? " + str(os.path.isdir(b))
print "b Is it a file? " + str(os.path.isfile(b))

The result after execution is:


a Does the file exist? True
a Is the path absolute? True
a Is it a directory? False
a Is it a file? True
=================================
b Does the file exist? False
b Is the path absolute? True
b Is it a directory? False
b Is it a file? False

We can imagine that 1, after the execution of these two paths, the results should be identical, of course, the results are not identical, because what?The result you just executed was ide, so let's try using Python's interactive command line


>>> a = "D:\\test\\test.txt"
>>> print "a Does the file exist? " + str(os.path.exists(a))
a Does the file exist? True
>>> print "a Is the path absolute? " + str(os.path.isabs(a))
a Is the path absolute? True
>>> print "a Is it a directory? " + str(os.path.isdir(a))
a Is it a directory? False
>>> print "a Is it a file? " + str(os.path.isfile(a))
a Is it a file? True
>>> b = "D:\ test \ test .txt"
>>> print "b Does the file exist? " + str(os.path.exists(b))
b Does the file exist? True
>>> print "b Is the path absolute? " + str(os.path.isabs(b))
b Is the path absolute? True
>>> print "b Is it a directory? " + str(os.path.isdir(b))
b Is it a directory? False
>>> print "b Is it a file? " + str(os.path.isfile(b))
b Is it a file? True

It's embarrassing at this point, and the results here are consistent, so it's either an ide problem or a coding problem.Experimentally proven to be a coding problem


import os
a = "D:\\test\\test.txt"
b = u"D:\ test \ test .txt"
 
print "a Does the file exist? " + str(os.path.exists(a))
print "a Is the path absolute? " + str(os.path.isabs(a))
print "a Is it a directory? " + str(os.path.isdir(a))
print "a Is it a file? " + str(os.path.isfile(a))
 
print "b Does the file exist? " + str(os.path.exists(b))
print "b Is the path absolute? " + str(os.path.isabs(b))
print "b Is it a directory? " + str(os.path.isdir(b))
print "b Is it a file? " + str(os.path.isfile(b))

results of enforcement


a Does the file exist? True
a Is the path absolute? True
a Is it a directory? False
a Is it a file? True
==========================
b Does the file exist? True
b Is the path absolute? True
b Is it a directory? False
b Is it a file? True

Okay, now 1 is consistent, you can see in the code that only one u has been added to the path of b (meaning that the string has been changed into an Unicode encoded string). So you can see how serious the Chinese encoding problem is in python2.7. This way of adding u only will certainly not work. It is recommended to use unicode method of Python to convert.

Resolvent:


b = "D:\ test \ test .txt"
b = unicode(b, 'utf-8')

Summary: The Chinese language problem should be a headache for python 2.7. More than 3 should be better. Don't worry about this problem. Then, when dealing with the path or other Chinese content, whether in English or Chinese, you can add unicode (content, encoding) step 1 to ensure that it is correct. I hope this article can help you.


Related articles: