Python Forum
Simple Python script, path not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Python script, path not defined
#1
Beginning python class, writing a simple script. I cannot figure out why my script is getting path not defined exception
Yoriz write Nov-06-2021, 04:46 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.py   SteveDubinaFIrstScript_Att3.py (Size: 2.96 KB / Downloads: 201)
Reply
#2
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
Reply
#3
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.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,269 Jun-29-2023, 11:57 AM
Last Post: gologica
  Simple Question - ' defined as "a". ?' Ryan012 10 1,639 May-27-2023, 06:03 PM
Last Post: Ryan012
  Script File Failure-Path Error? jerryf 13 3,473 Nov-30-2022, 09:58 AM
Last Post: jerryf
  Script Path tfernandes 8 2,209 Jul-07-2022, 09:48 PM
Last Post: tfernandes
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,220 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  “NameError” “is not defined” script that I know works on other computers iamwithstupid 0 2,529 Nov-24-2020, 04:23 PM
Last Post: iamwithstupid
  Need help creating a simple script Nonameface 12 4,566 Jul-14-2020, 02:10 PM
Last Post: BitPythoner
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,926 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  python library not defined in user defined function johnEmScott 2 3,879 May-30-2020, 04:14 AM
Last Post: DT2000
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,312 May-28-2020, 05:27 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020