adamshields
asked on
Fixing function call in Python script
I am trying to correctly reference the fpath function in the script for use by the last code block. Currently the traceback specifies NameError: global name 'all_files' is not defined.
Any tips would be great.
Any tips would be great.
#! /usr/bin/env python
import os, sys, glob
from optparse import OptionParser
print "ARGS", sys.argv
def fpath(arguments):
files = []
for arg in arguments:
if '*' in arg or '?' in arg:
# contains a wildcard character
all_files.extend(glob.glob(arg))
elif os.path.isdir(arg):
# is a dictionary
all_files.extend(glob.glob(os.path.join(arg, '*')))
elif os.path.exists(arg):
# is a file
all_files.append(arg)
else:
# invalid?
print '%s invalid' % arg
return files
def main():
usage = "usage: %prog [options] -f filename"
parser = OptionParser(usage)
parser.add_option('-d', '--download',
action='store', dest='download',
default=None, help='download files from cloud')
parser.add_option('-f', '--file',
action='store', dest='filename',
default=None, help='specify file or wildcard')
if len(sys.argv) == 1:
parser.print_help()
sys.exit()
(options, args) = parser.parse_args()
# List files in directory and upload them to bucket
all_files = ''
all_files = fpath(options.filename)
for filename in all_files:
#skip all directory entries which are not a file
if not os.path.isfile(filename):
continue
k.set_contents_from_filename(filename, cb=percent_cb, num_cb=10)
if __name__ == '__main__':
main()
My guess is that you are trying to append to "all_files" in line 19, but it is not in scope.
ASKER
So what needs to be done to make this work?
You declared the variable files inside of fpath(), but all you do is return it--you don't add anything to it. I think you meant to use files instead of all_files inside of fpath(). Try this:
def fpath(arguments):
files = []
for arg in arguments:
if '*' in arg or '?' in arg:
# contains a wildcard character
files.extend(glob.glob(arg))
elif os.path.isdir(arg):
# is a dictionary
files.extend(glob.glob(os.path.join(arg, '*')))
elif os.path.exists(arg):
# is a file
files.append(arg)
else:
# invalid?
print '%s invalid' % arg
return files
ASKER
With those changes the script runs but the function does not seem to be working correctly. It is parsing the working directory and not any one of the three args.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great, that fixed that. One last question on creating functions. Would the following be the best way to make the last block of code a function?
def download(all_files):
for filename in all_files:
#skip all directory entries which are not a file
if not os.path.isfile(filename):
continue
k.set_contents_from_filename(filename, cb=percent_cb, num_cb=10)
#call the function for execution
download()
The definition appears OK, but the way you call it does not. You declared the function to take 1 parameter, but you are calling it with none. Pass the appropriate object in the call and you should be fine.
e.g.
e.g.
#call the function for execution
# based on what your code looks like
download(all_files)
ASKER
The download() function contains a reference to the fname() funciton, is there a way to handle args without defining globally?
Traceback:
Traceback (most recent call last):
File "./backup.py", line 109, in <module>
main()
File "./backup.py", line 95, in main
download(all_files)
File "./backup.py", line 33, in download
all_files = fname(args)
NameError: global name 'args' is not defined
def download(all_files):
all_files = fname(args)
for filename in all_files:
#skip all directory entries which are not a file
if not os.path.isfile(filename):
continue
k.set_contents_from_filename(filename, cb=percent_cb, num_cb=10)
Traceback:
Traceback (most recent call last):
File "./backup.py", line 109, in <module>
main()
File "./backup.py", line 95, in main
download(all_files)
File "./backup.py", line 33, in download
all_files = fname(args)
NameError: global name 'args' is not defined
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
NP. Glad to help = )