Python Forum

Full Version: Simple Python script, path not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Beginning python class, writing a simple script. I cannot figure out why my script is getting path not defined exception
Error

WK-2 Attempt: Steve K Dubina - Version 1.e

Week 2 Assignment: Steve Dubina Version 1.e

Enter a Directory Path i.e. c:/ >>> d:

========================================
$RECYCLE.BIN


Script Aborted Exception = name 'path' is not defined
Don't catch exceptions, let them propagate to see where the error comes from. For example, add a raise statement after the last print.

And, as Yoriz wrote above, please post the code between bbcode tags.
There are serval problems in code,you should test code in smaller parts and without exceptions as mention(want to see all errors when testing stuff out).
To take out part and do a quick test,timeLastAcess spell error in macTimeList.
import os       # File System Methods
import sys      # System Methods
import time     # Time Conversion Methods

def GetFileMetaData(fileName):
    '''
        obtain filesystem metadata
        from the specified file
        specifically, fileSize and MAC Times

        return True, None, fileSize and MacTimeList
    '''
    metaData         = os.stat(fileName)        # Use the stat method to obtain meta data
    fileSize         = metaData.st_size         # Extract fileSize and MAC Times
    timeLastAccess   = metaData.st_atime
    timeLastModified = metaData.st_mtime
    timeCreated      = metaData.st_ctime

    macTimeList = [timeLastModified, timeLastAccess, timeCreated] # Output MAC Times in a List
    return True, None, fileSize, macTimeList

if __name__ == '__main__':
    targetDIR = r'G:\div_code\outdir'
    fileList = os.listdir(targetDIR)
    for eachEntry in fileList:
        print("="*40)
        #print(eachEntry)
        fullPath = os.path.join(targetDIR, eachEntry)
        print(fullPath)
        success, errInfo, fileSize, macList = GetFileMetaData(fullPath)
Output:
======================================== G:\div_code\outdir\1.txt ======================================== G:\div_code\outdir\2.txt ======================================== G:\div_code\outdir\boy_1.txt ======================================== G:\div_code\outdir\boy_2.txt ======================================== G:\div_code\outdir\holdem_calc-1.0.0 ======================================== G:\div_code\outdir\multi_files.py
So iterating over file works.
Line 30 here will only get info of last file in the loop.
success, errInfo, fileSize, macList = GetFileMetaData(fullPath)
>>> success
True
>>> errInfo
>>> fileSize # Only last file in loop,all info of files before is now lost
586
>>> macList
[1628441571.5508275, 1630179742.932314, 1628440434.2352946] # Only last file in loop