File Information Viewer

Source: Internet
Author: User
#-*-Coding: cp936 -*-
'''File information viewer, which displays the file path, size, creation date, last modification date, and last access date '''
Gcurrentdir =''
From tkinter import *
Import OS
Root = TK ()

Label (root, text = 'file operation demo'). Grid (ROW = 0, column = 0, columnspan = 3)
Label (root, text = 'directory'). Grid (ROW = 1, column = 0, stick = W)
Label (root, text = 'file'). Grid (ROW = 1, column = 1, stick = W)
Label (root, text = 'file Properities '). Grid (ROW = 1, column = 2, stick = W)

# Obtain the current directory location
Gcurrentdir = OS. getcwd ()

Def refreshdirs (lbdirs _, curdir ):
''' Update the directory list
1. Delete all records
2. Insert the current directory list
3. Select the first item.
'''
Lbdirs _. Delete (0, end)
Lbdirs _. insert (0 ,'.')
Lbdirs _. insert (1 ,'..')
For item in (OS. listdir (curdir )):
If OS. Path. isdir (curdir + (OS. SEP) + item ):
Lbdirs _. insert (end, item)
Lbdirs _. selection_set (0)
Def refreshfiles (lbfiles _, curdir ):
''' Update the file list
1. Delete all records
2. Insert the file list of the current directory
3. Select the first item.
'''
Lbfiles _. Delete (0, end)
For item in (OS. listdir (curdir )):
If OS. Path. isfile (curdir + (OS. SEP) + item ):
Lbfiles _. insert (end, item)
Lbfiles _. selection_set (0)


Def changedir (event ):
''' When you double-click the listbux directory,
1. Change the current path. If it is '.', it will not be processed. If it is '.', remove the final item,
2. Update the file list at the same time.
3. Update the file display attribute '''
Global gcurrentdir
Sel = lbdirs. Get (lbdirs. curselection ())
If sel = '.':
Return
Elif sel = '..':
Gcurrentdir = OS. Path. Split (gcurrentdir) [0]
Print gcurrentdir
Else:
Gcurrentdir = gcurrentdir + OS. SEP + Sel
Print gcurrentdir
OS. chdir (gcurrentdir)
Refreshdirs (lbdirs, gcurrentdir)
Refreshfiles (lbfiles, gcurrentdir)
Showproperties (Event)

# Creating a directory list
Lbdirs = ListBox (Root)
Refreshdirs (lbdirs, gcurrentdir)
Lbdirs. Grid (ROW = 2, column = 0, stick = W)
Lbdirs. BIND ('<double-button-1>', changedir)

Def showproperties (event ):
''' Is used to display object attributes.
1. File Path
2. File Size
3. file creation date
4. File modification date
5. File Access date '''
Import time
Try:
# Note that the current directory does not contain files. Exception Handling is used here, and the information is displayed blank.
Fn = gcurrentdir + OS. SEP + lbfiles. Get (lbfiles. curselection ())
Print 'fn = ', FN
State = OS. Stat (FN)
MSG =''
MSG = 'location: '+ FN +''
MSG = MSG + 'size: '+ (' % d' % State [-4]) +''
T = time. localtime (State [-1])
MSG = MSG + 'create: '+
('% D/% d: % d' %
(T [0], t [1], t [2], t [3], t [4], t [5]) +''
T = time. localtime (State [-2])
MSG = MSG + 'modify: '+
('% D/% d: % d' %
(T [0], t [1], t [2], t [3], t [4], t [5]) +''
T = time. localtime (State [-3])
MSG = MSG + 'Access: '+
('% D/% d: % d' %
(T [0], t [1], t [2], t [3], t [4], t [5]) + ''# OS. linesep

Lblproperities ['text'] = msg
Except t:
MSG =''
Lblproperities ['text'] = msg

# Create a file list
Lbfiles = ListBox (Root)
Refreshfiles (lbfiles, gcurrentdir)
Lbfiles. Grid (ROW = 2, column = 1, stick = W)
Lbfiles. BIND ('<double-button-1>', showproperties)

# Creating attribute tags
Lblproperities = message (Root)
Showproperties (0)
Lblproperities. Grid (ROW = 2, column = 2)
Root. mainloop ()

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.