首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python2.7和PrettyTables

Python2.7和PrettyTables
EN

Stack Overflow用户
提问于 2018-06-18 04:37:49
回答 1查看 390关注 0票数 1

我正在尝试让PrettyTables使用以下脚本。我可以让它看起来几乎正确,但它一直在分隔我的表,所以它打印了16个不同的表。我需要将所有信息放在一个可以排序的表中。我感谢所有我能得到的帮助。

代码语言:javascript
复制
import sys
import os
import datetime
import hashlib
import logging

def getScanPath(): #12
# Prompt User for path to scan
    path = raw_input('Please enter the directory to scan:  ')

# Verify that the path is a directory
    if os.path.isdir(path):
        return path
    else:
        sys.exit('Invalid File Path ... Script Aborted')

def getFileList(filePath):
# Create an empty list to hold the resulting files
    pathList =[]
# Get a list of files, note these will be just the names of the files
# NOT the full path
    simpleFileNameList = os.listdir(filePath)

# Now process each filename in the list 
    for eachFile in simpleFileNameList:
    # 1) Get the full path by join the directory with the filename
        fullPath = os.path.join(filePath, eachFile)
    # 2) Make sure the full path is an absolute path
        absPath = os.path.abspath(fullPath)
    # 3) Make sure the absolute path is a file i.e. not a folder or directory 
        if os.path.isfile(absPath):
        # 4) if all is well, add the absolute path to the list
            pathList.append(absPath)

        else:
            logging.error('A Non-File has been identified')

# 5) Once all files have been identified, return the list to the caller 
    return pathList 

def getFileName(theFile):
    return os.path.basename(theFile)
def getFileSize(theFile):
    return os.path.getsize(theFile)
def getFileLastModified(theFile):
    return os.path.getmtime(theFile)
def getFileHash(theFile):
    hash_md5 = hashlib.md5()
    with open(theFile, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()

 # Main Script Starts Here

 if __name__ == '__main__':
#Welcome Message
    print "\nWelcome to the file scanner\n"

# prompt user for directory path
    scanPath = getScanPath()

# Get a list of files with full path
    scanFileList = getFileList(scanPath)

# Output Filenames
    print "Files found in directory"
    for eachFilePath in scanFileList:
        fileName      = getFileName(eachFilePath)
        fileSize      = getFileSize(eachFilePath)
        lastModified  = getFileLastModified(eachFilePath)
        hashValue     = getFileHash(eachFilePath)
        fileModified  = (datetime.datetime.fromtimestamp(lastModified))

        from prettytable import PrettyTable

        pTable = PrettyTable()

        pTable.field_names = ["File Name", "File Size", "Last Modified", "Md5 Hash Value"]

        pTable.add_row ([fileName, fileSize, fileModified, hashValue])

        print (pTable)enter code here

这应该会显示一个很大的表,其中包含用户选择的设置目录中的所有值。这将允许我稍后使用prettytables对表进行排序。

EN

回答 1

Stack Overflow用户

发布于 2018-06-18 06:32:17

我没有使用prettyTables的经验,但是我注意到您有lastModifiedfileModified,但是只有fileModified用于表中的列。你确定漂亮的表没有某种行数限制吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50900290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档