Home>
I want to know

I want to read multiple csv files in D: \ Desktop \ recipe, but I don't know how to do it. I tried to write it as below, but I get an error. How to write a csv file

# File name: otamesii.py
import os
import csv
from getenc import getEncode
fileList = os.listdir ('D: \\ Desktop \\ recipe')
for j in range (len (fileList)):
    f = open ('D: \\ Desktop \\ recipe \\' + fileList [j], 'r', encoding = 'utf-8')
    CSVFILE = f
    # Can read even if the character code is unknown
    enc = getEncode (CSVFILE)
    with open (CSVFILE, 'r', encoding = enc) as csvfile:
        csv_reader = csv.reader (csvfile, delimiter = ',', quotechar = '"')
        for row in csv_reader:
            moji = row


The getenc.py used in the above program is as follows.

#!/usr/bin/python3
# (File name: getenc.py)
# Lincense: CC0
#http: //myfuturesightforpast.blogspot.jp/2015/01/auto-detecting-japanese-file-enconding.html
#How to use
# $./getenc.py 27OSAKA.CSV
# $./getenc.py Text.txt
#How to use (when calling)
#from getenc import getEncode
#
#getEncode (file name)
#Character code automatic judgment function
def getEncode (filepath):
    encs = "iso-2022-jp euc-jp shift_jis utf-8" .split ()
    for enc in encs:
        with open (filepath, encoding = enc) as fr:
            try:
                fr = fr.read ()
            except UnicodeDecodeError:
                continue
        return enc
if __name__ == '__main__':
    import sys
    argv = sys.argv
    argc = len (argv)
    hikisuu = 1
    if argc<= hikisuu:
        print ("Specify one text file name to check character encoding as an argument.")
        print ("Usage")
        print ("$./getenc.py text file.CSV (txt)")
    else:
        FR = argv [1]
        print (getEncode (FR))


Addendum
I tried the code you answered and got an error.
What's wrong with the code below?

import os
import csv
# import getenc module
from getenc import getEncode
fileList = os.listdir ('D: \\ Desktop \\ recipe')
for j in range (len (fileList)):
    file_name = 'D: \\ Desktop \\ recipe \\' + fileList [j]
    f = open (file_name, 'r', encoding = 'utf-8')
    # Can read even if the character code is unknown
    enc = getEncode (f)
    f.close ()
    with open (file_name, 'r', encoding = enc) as csvfile:
        csv_reader = csv.reader (csvfile, delimiter = ',', quotechar = '"')
        for row in csv_reader:
            cut = row
Error details
File "where the file is located \ otamesii.py", line 11, in<module>
    enc = getEncode (CSVFILE)
  File "where the file is located \ getenc.py", line 17, in getEncode
    with open (filepath, encoding = enc) as fr:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
  • Answer # 1

    enc = getEncode (file_name)
    And it was done.
    I don't think it was necessary to open the file.

  • Answer # 2

    f = open ('D: \\ Desktop \\ recipe \\' + fileList [j], 'r', encoding = 'utf-8 ')
        CSVFILE = f
        # Can read even if the character code is unknown
        enc = getEncode (CSVFILE)
        with open (CSVFILE, 'r', encoding = enc) as csvfile:

    In this case,fis already a file object and cannot be given as an argument toopen. If you're dealing with presumed encoding

    file_name = 'D: \\ Desktop \\ recipe \\' + fileList [j]
        f = open (file_name, 'r', encoding = 'utf-8')
        # Can read even if the character code is unknown
        enc = getEncode (f)
        f.close () # Close encoding after estimation
        with open (file_name, 'r', encoding = enc) as csvfile:
    I think it will look like

    .