Home > other >  In PYTHON statistics given all the total number of rows of a text file folder
In PYTHON statistics given all the total number of rows of a text file folder

Time:09-17

# - * - coding: utf-8 - * -
The import OS

Def listdir (path, list_name) : # to store a list
For file in OS. Listdir (path) :
File_path=OS. Path. Join (path, file)
If OS. Path. Isdir (file_path) :
Listdir (file_path list_name)
The else:
List_name. Append (file_path)

FilesDir=r "D: \ aa, bb, cc, dd"
FilesList=[]
Listdir (filesDir filesList)
# print (filesList)
Count=0 # number of rows
For file in filesList:
Print (file)
F=open (the file, the "r")
For the line in f.r eadlines () :
The count=count + 1
Print (" code line number="+ STR (count))




The above code to run, the tip UnicodeDecodeError: 'GBK' codec can 't decode byte 0 xab position in 85: illegal multibyte sequence



Code is written by others, is solved

CodePudding user response:

The given text file using GBK code, utf-8 don't know, can't read,
Solution: f.r eadlines (.) encode (' GBK) specified encoding, if there are print content requirements, test code format,
GBK encoding, usually used gb2312 utf-8.

CodePudding user response:

Code is wrong
  • Related