Je suis curieux de définir une variable en utilisant optparse. Je cours le programme en tant que tel;Définition d'une variable avec optparse en Python
programname.py -dc: \ users \\ etc \ etc \ etc
Je veux être en mesure d'utiliser -d C: \ Users \\ etc \ etc pour remplir une variable appelée, "chemin" , que j'utilise plus tard dans le programme. Cela peut-il être fait? Voici le code optionparser que j'ai. J'appelle plus tard la variable Path, que j'utilise pour remplir un dictionnaire.
erreur que je reçois est:
E:> japp_id.py -d "C: \ Users \\ AppData \ Roaming \ Microsoft \ Windows \ \ récents AutomaticDestinations" retraçage (appel le plus récent en dernier): fichier "E: \ japp_id.py", ligne 30, à pour ids à os.listdir (chemin): NameError: nom 'path' est pas défini
try:
import os
import sys
import urllib2
from BeautifulSoup import BeautifulSoup
from optparse import OptionParser
except ImportError:
print 'Imports not installed'
sys.exit()
def main():
usage = "usage: %prog [options] arg1 arg2"
parser = OptionParser()
parser.add_option("-d", "--default", action="callback", type="string", dest="dpath")
(opts, args) = parser.parse_args()
if opts.dpath == None:
parser.print_help()
parser.error("You must supply a -d for dpath")
if not os.path.isfile(opts.dpath):
parser.error("%s does not exist" % opts.dpath)
if __name__ == "__main__":
main()
appIDlist = []
for ids in os.listdir(path):
appid = "%s" % (ids).rstrip('.automaticDestinations-ms')
appIDlist.append(str(appid))
f = urllib2.urlopen("http://www.forensicswiki.org/wiki/List_of_Jump_List_IDs")
s = f.read()
soup = BeautifulSoup(''.join(s))
rows = soup.findAll('tr')
appIDdictionary = dict() #create an empty dictionary to allow lookups {'appID':'processName'}
for tr in rows: #iterate through every row in the table
cols = tr.findAll('td', limit=2) #get the first two data chunks (<td>'s)
appID = str(cols[0]).rstrip('</td>').lstrip('<td>') #clean up formatting
processName = str(cols[1]).rstrip('</td>').lstrip('<td>') #clean up formatting
appIDdictionary[appID] = processName #add the pair to the dictionary
#iterate through list of appids pulled from windows user profile, look them up in the dictionary, and print the result
for id in appIDlist:
if id in appIDdictionary:
print appIDdictionary[id]# + " is " + ids.rstrip('.automaticDestinations-ms')
else:
print 'Unable to find ' + id + ' in dictionary.'
f.close()
Donc voici mon code final. Dites-moi où vous en pensez. – mrwh1t3