Display Reorder File Names

Project #1

Create a interactive program that:

Project #2

Same as Project #1 but also display the meaning of each bit in a file's "st_mode". (See the output format of the Linux/Unix "ls" command.)

Project #3

Compares filenames from two different directories. If there is a filename match, display the filenames if they have different modes. (And how they are different?)

Example Code

# ------------------------------------------------------------ # difference between two lists (list1 - list2) # ------------------------------------------------------------ def difference_in_lists(list1,list2): diff = [] for element in list1: if element not in list2: diff.append(element) return diff

#!/usr/bin/python3 # ============================================================ # file stat data # ============================================================ import datetime import os import stat description = '''\ ----stat description ---------------------- st_mode = mode bits st_ino = inode number st_dev = device st_nlinks = number of hard links st_uid = user id st_gid = group id st_size = size of file in bytes atime = most recent access mtime = most recent modification ctime = most recent metadata change -------------------------------------------''' # ------------------------------------------------------------ # ---- get a list of the filenames in a directory (skip links) # ------------------------------------------------------------ def get_list_of_files(dir): # --- get a list of entries in the directory entries = os.listdir(dir) # --- collect all of the filenames in the directory files = [] for f in entries: ff = dir + '/' + f # path + filename if os.path.isfile(ff) if not os.path.islink(ff): files.append(f) files.sort() return files # ------------------------------------------------------------ # ---- date/time string from timestamp # ------------------------------------------------------------ def timestamp_string(timestamp): s = datetime.datetime.fromtimestamp(timestamp) return s.strftime('%Y-%m-%d %H:%M:%S') # ------------------------------------------------------------ # ---- stat mode string (file protection bits) # ------------------------------------------------------------ def stat_mode_string(stat_mode,human_readable=True): # ---- file’s mode as a human readable string if human_readable: return stat.filemode(stat_mode) # ---- file’s mode bits as a string b = bin(stat_mode)[2:] b1 = b[0:4] b2 = b[4:8] b3 = b[8:12] b4 = b[12:16] bb = b1 + ' ' + b1 + ' ' + b3 + ' ' + b4 return bb # ------------------------------------------------------------ # ---- display file stat data # ------------------------------------------------------------ def display_file_stat(filename,stats): print(f'file = {filename}') mode = stats.st_mode print(f'st_mode = {stat_mode_string(mode)}') print(f'st_inode = {stats.st_ino}') print(f'st_dev = {stats.st_dev}') print(f'st_nlink = {stats.st_nlink}') print(f'st_uid = {stats.st_uid}') print(f'st_gid = {stats.st_gid}') print(f'st_size = {stats.st_size} bytes') s = timestamp_string(stats.st_atime) print(f'st_atime = {s}') s = timestamp_string(stats.st_mtime) print(f'st_mtime = {s}') s = timestamp_string(stats.st_ctime) print(f'st_ctime = {s}') return # ------------------------------------------------------------ # ---- main # ------------------------------------------------------------ if __name__ == '__main__': dir_old = 'd:/abc' dir_new = 'd:/xyz' filename = 'picture.png' old_files = get_list_of_files(dir_old) new_files = get_list_of_files(dir_new) print() print(f'{len(old_files)} files found in old dir') print(f'{len(new_files)} files found in new dir') # ---- display description of file stat values print() print(description) # ---- get file stats file = dir_old + '/' + filename # ---- get and display file stats stats = os.stat(file) print() display_file_stat(filename,stats)

Links

Different Types of Files in Linux

inode (Wikipedia)

File-system permissions (Wikipedia)

chmod (Wikipedia)

Python os.stat() method

stat — Interpreting stat() results