Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name not defined error
#1
in below program, i try to zip the files under 1139 directory.

import os
import datetime
import zipfile

def zipt(folder):
 folder = os.path.abspath(folder)
 ver = 1
 while True:
  zip_f = os.path.basename(folder)+'_'+str(ver)+'.zip'
  if not os.path.exsits(zip_f):
    break
  ver += 1
print("\narchiving %s..."%(zip_f))
zip_file=zipfile.ZipFile(zip_f,'w')
for foldername,subfolders,filenames in os.walk(folder):
 print("\nadding files in the %s..."%(foldername))
 zip_file.write(foldername)
 for filename in filenames:
  new_base=os.path.basename(folder)+'_'
  if filename.startswith(new_base) and filename.endswith('.zip'):
    continue
  zip_file.write(os.path.join(foldername,filename))
 zip_file.close()
 print("\narchiving complete, {} created successfully".format(zip_f))
 
 zipt('c:\\dell\\1139')
but it runs with error :
Traceback (most recent call last):
File "C:\dell\test_zip.py", line 13, in <module>
print("\narchiving %s..."%(zip_f))
NameError: name 'zip_f' is not defined

my zip_f is defined with os.path.basename(folder)+'_'+str(ver)+'.zip', why still error? thanks
Reply
#2
it's defined and value is assigned within zipt function. It has local scope, i.e. that zip_f it's not visible outside the function. One option (not recommended) is to declare it global variable, so you can change it from within the function, i.e. changes you made in the function will have effect also outside the function. The better option is to return the value from the function and assign it to a variable zip_f. In both cases it will not work till you actually call and execute the function, which you do in the last line. i.e. you will need to move that line before you are able to use zip_f name
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 517 Nov-23-2023, 02:53 PM
Last Post: rob101
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,045 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error 'Contour' not Defined DaveG 3 2,283 Mar-13-2022, 03:29 AM
Last Post: deanhystad
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,529 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,335 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 2,123 Dec-14-2020, 05:45 PM
Last Post: karabakh
  name error "name"is not defined MaartenRo 1 3,377 Jul-28-2020, 02:39 AM
Last Post: bowlofred
  Name Error: name 'Stockton' is not defined Pinokchu 3 2,249 Jun-13-2020, 02:48 PM
Last Post: Yoriz
  python library not defined in user defined function johnEmScott 2 3,775 May-30-2020, 04:14 AM
Last Post: DT2000
  error ,,name append is not defined'' Killdoz 1 4,985 May-24-2020, 06:23 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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