Spit out a list of file name, last modified date and character code in python3

anaconda3 installed For anaconda, see Install anaconda on Mac

Install chardet that determines the character code

--If you don't have anaconda installed, pip install or install anaconda --Install chardet

zsh


$ anaconda search -t conda chardet #Find chardet in anaconda
Using Anaconda API: https://api.anaconda.org
Run 'anaconda show <USER/PACKAGE>' to get more details:
Packages:
     Name                      |  Version | Package Types   | Platforms
     ------------------------- |   ------ | --------------- | ---------------
     anaconda/chardet          |    3.0.4 | conda           | linux-ppc64le, linux-64, win-32, osx-64, linux-32, win-64
...

$ anaconda show anaconda/chardet #Select the one you like from the chardet you searched for and find out how to install it
Using Anaconda API: https://api.anaconda.org
Name:    chardet
Summary:
Access:  public
Package Types:  conda
Versions:
   + 2.3.0
   + 3.0.2
   + 3.0.3
   + 3.0.4

To install this package with conda run:
     conda install --channel https://conda.anaconda.org/anaconda chardet

$ conda install --channel https://conda.anaconda.org/anaconda chardet #Copy and paste the installation command that came out after checking
Fetching package metadata .........
Solving package specifications: ..........

Package plan for installation in environment /Users/berry/.pyenv/versions/anaconda3-4.2.0:

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    conda-env-2.6.0            |                0          601 B  anaconda
    chardet-3.0.4              |           py35_0         188 KB  anaconda
    requests-2.14.2            |           py35_0         725 KB  anaconda
    pyopenssl-16.2.0           |           py35_0          70 KB  anaconda
    conda-4.3.22               |           py35_0         516 KB  anaconda
    ------------------------------------------------------------
                                           Total:         1.5 MB

The following NEW packages will be INSTALLED:

    chardet:   3.0.4-py35_0  anaconda
    conda-env: 2.6.0-0       anaconda

The following packages will be UPDATED:

    conda:     4.2.9-py35_0           --> 4.3.22-py35_0 anaconda
    pyopenssl: 16.0.0-py35_0          --> 16.2.0-py35_0 anaconda
    requests:  2.11.1-py35_0          --> 2.14.2-py35_0 anaconda

Proceed ([y]/n)? y

Fetching packages ...
conda-env-2.6. 100% |############################################################################| Time: 0:00:00 304.59 kB/s
chardet-3.0.4- 100% |############################################################################| Time: 0:00:02  83.22 kB/s
requests-2.14. 100% |############################################################################| Time: 0:00:52  14.02 kB/s
pyopenssl-16.2 100% |############################################################################| Time: 0:00:02  25.32 kB/s
conda-4.3.22-p 100% |############################################################################| Time: 0:00:18  28.66 kB/s
Extracting packages ...
[      COMPLETE      ]|###############################################################################################| 100%
Unlinking packages ...
[      COMPLETE      ]|###############################################################################################| 100%
Linking packages ...
[      COMPLETE      ]|###############################################################################################| 100%

$ 

Export the file name, modification date and time, and character code to csv

--Export the list of files under './file/' to csv

python3


# support python3

import os
import datetime
import csv
from chardet.universaldetector import UniversalDetector

_path = './file/'

# get encode of a file
def univ_detect(file_dir):
    ud = UniversalDetector()
    with open(file_dir, 'rb') as fd:
        for b in fd:
            ud.feed(b)
            if ud.done:
                break
    ud.close()
    return ud.result['encoding']

# get a file name, path and last-modified timestamp
all_files = []
def get_file_list(file_path):
    # all_files = []
    file_list = [f for f in os.listdir(file_path)]
    for g in file_list:
        g_path = os.path.join(file_path, g)

        # last modified time        
        last_modified = os.path.getmtime(g_path)
        dt = datetime.datetime.fromtimestamp(last_modified).strftime('%Y%m%d_%H:%M:%S')

        # chardet
        encode = 'Directory'
        if os.path.isdir(g_path):
            pass
        else:
            encode = univ_detect(g_path)

        all_files.append([dt, g_path.split(_path,1)[1], ' '.join(['~',encode,'~'])])

        # subdirectory
        if os.path.isdir(g_path):
            subfile_list = [i for i in os.listdir(g_path)]
            for j in subfile_list:
                j_path = os.path.join(g_path, j)

                # last modified time
                sub_last_modified = os.path.getmtime(j_path)
                sub_dt = datetime.datetime.fromtimestamp(sub_last_modified).strftime('%Y%m%d_%H:%M:%S')

                # chardet
                encode = 'Directory'
                if os.path.isdir(j_path):
                    pass
                else:
                    encode = univ_detect(j_path)

                all_files.append([sub_dt, j_path.split(_path,1)[1], ' '.join(['~',encode,'~'])])
    return file_list
    # return all_files

print(get_file_list(_path))

csv_file = [['Last_modified', 'file_path', 'encode']] # Header
csv_file.extend(all_files)
with open('file_checker.csv', 'w') as h:
    writer = csv.writer(h, lineterminator='\n')
    writer.writerows(csv_file)

Recommended Posts

Spit out a list of file name, last modified date and character code in python3
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.
Display a list of alphabets in Python 3
[Python] Get the character code of the file
Make a copy of the list in Python
[Python] [chardet] Automatic detection of character code of file
Links and memos of Python character code strings
List of Python code to move and remember
Convert the character code of the file with Python3
Various ways to read the last line of a csv file in Python
[Python] Create a list of date and time (datetime type) for a certain period
List of main probability distributions used in machine learning and statistics and code in python
A collection of code often used in personal Python
List of Python code used in big data analysis
Group by consecutive elements of a list in Python
Sample of getting module name and class name in Python
Summary of date processing in Python (datetime and dateutil)
[Python] How to make a list of character strings character by character
[Python] Create a date and time list for a specified period
Get the number of specific elements in a python list
[Python] How to delete rows and columns in a table (list of drop method options)
[Note] Import of a file in the parent directory in Python
2.x, 3.x character code of python
Decrypt one line of code in Python lambda, map, list
Create code that outputs "A and pretending B" in python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Parse the Researchmap API in Python and automatically create a Word file for the achievement list
Python: Create a dictionary from a list of keys and values
Google search for the last line of the file in Python
Character code learned in Python
Command to list all files in order of file name
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
Character code processing compared to "Are to put on a wound" ~ Garbled file name operation with python3 ~
Set the last modified date of the child file to the modified date of the parent directory
Try to get a list of breaking news threads in Python.
Delete a particular character in Python if it is the last
Python vba to create a date string for creating a file name
List of Linear Programming (LP) solvers and modelers available in Python
Dig the directory and create a list of directory paths + file names
Get a list of files in a folder with python without a path
Get the title and delivery date of Yahoo! News in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Create a binary file in Python
Write a C language linked list in an object-oriented style (with code comparison between Python and Java)
Replace the directory name and the file name in the directory together with a Linux command.
Get a list of packages installed in your current environment with python
[Python] How to put any number of standard inputs in a list
[Python] Summary of conversion between character strings and numerical values (ascii code)
Test & Debug Tips: Create a file of the specified size in Python
Python CSV file Character code conversion, file name extraction, reading, output, merging operation
python> Copy and paste code> SyntaxError: Non-ASCII character'\ xe3' in file>-*-coding: utf-8-*-Add
The file name was bad in Python and I was addicted to import
Receive a list of the results of parallel processing in Python with starmap
Open an Excel file in Python and color the map of Japan
python> Get file last modified date> mddt = time.ctime (os.path.getmtime (filepath))> Mon Mar 7 07:18:45 2016
How to format a list of dictionaries (or instances) well in Python
I made a program to check the size of a file in Python
Code reading of faker, a library that generates test data in Python
Code reading of Safe, a library for checking password strength in Python