issue with 'path' on python script

so i have a python script that parses through a folder on disk, opens every TXT file, strips out the email addresses within the file, then appends the email address to the master file... this used to work well but not i keep getting an error/issue with the path arg that was passed to the script...

#!/usr/bin/python

#Import the necessary modules for use later:
import sys, os, fileinput, glob, datetime

def emailparser(directory, master):
	#Create a list of the locations of all of the bounceback e-mails:
	dirlist=glob.glob(os.path.join(str(directory),'*.txt'))

	#Create a list of the target e-mail addresses in each bounceback
	#from the line in the bounceback that begins "To:":
	bouncers=[email[3:].strip() for email in fileinput.input(dirlist) if email[0:3]=="To:"]

	#Open the master list of e-mails and strip each e-mail
	#of newline characters:
	masterlist=[email.strip() for email in open(str(master)).readlines()]

	#Create a new master list of e-mails by removing those that are in
	#the bouncer list and adding newline characters for later file writing:
	newmasterlist=[email+"\r\n" for email in masterlist if email not in bouncers]

	#Get today's date:
	today=str(datetime.date.today())
	
	#Create a new master e-mail file name with today's date appended:
	newmasterfilename=os.path.join(os.path.dirname(master),'master_list_emails.{0}.txt'.format(today))
	
	#Open the new master file for writing:
	newmasterfile=open(newmasterfilename,'w')
	
	#Write the new master list to the file:
	newmasterfile.writelines(newmasterlist)
	
	#Close the new master file:
	newmasterfile.close()

	#Some dialogue to confirm successful execution:
	print "Old e-mail list size: ", len(masterlist)
	print "New e-mail list size: ", len(newmasterlist)
	print "E-mails pruned: ", len(masterlist)-len(newmasterlist)
	print "Location of new master list:", newmasterfilename
	

if __name__=="__main__":
	import sys, os
	try:
		emailparser(directory=str(sys.argv[1]), master=str(sys.argv[2]))
	except:
		print "You did not enter the absolute path to the directory of the bounced e-mail files and/or the path to the master e-mail list."
		print "Please run the command in the following form (in the path formatting of your platform):"
		print "python emailparser.py '/Path/to/directory/' '/Path/to/file/master_list_emails.txt'"
	

Open in new window

then to run you pass the params for the DIR and the File

 python emailparser.py '/home/email_parser/bounced/' '/home/email_parser/bounced/master_list_emails.txt'

Open in new window


when i run it i get the "path not found"
LVL 3
Timothy GoldenWeb DevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Timothy GoldenWeb DevAuthor Commented:
wha ti really want to do is parse through 6000+ bounced emails from my mail damon and remove them from my excel data file... so this python script used to work well but for some dum reason it wont any more...

so if there is a better way to parse through 6000+ .EML files and extract the the email address into another file.. please indulge me... either way, making this script work, or making a new script that does the same thing would be what i'm looking to get fixed..

the underlying issue here is that i have 6000+ .EML bounced emails in my outlook that i want to find an easy way to parse through them and generate a list of the 6000+ email so i can remove them from my database..
0
Hamed ZaghaghiProgrammerCommented:
Hi,

Is directory path contains space?
What Error do you get exactly?
0
Timothy GoldenWeb DevAuthor Commented:
the path has no spaces... the error is in the try/catch..
path = /home/email_parser/bounced/

Open in new window


error
You did not enter the absolute path to the directory of the bounced e-mail files and/or the path to the master e-mail list.
Please run the command in the following form (in the path formatting of your platform):
python emailparser.py '/Path/to/directory/' '/Path/to/file/master_list_emails.txt'
	

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

HonorGodSoftware EngineerCommented:
You could add some code to verify the user input.

For example,
  if os.path.exists( directory ) and os.path.isdir( directory ) :
    dirlist=glob.glob( os.path.join( directory, '*.txt' ) )
  else :
    print "Input error - specified location doesn't exist, or isn't a directory:", directory

Open in new window


Note: You don't need to use str(sys.argv[1]) since sys.argv is a list/array of strings.
0
Timothy GoldenWeb DevAuthor Commented:
i will try taht code.. BUT i am putting the full path in correctly
0
Hamed ZaghaghiProgrammerCommented:
Please, remove try except block so that we can see the actual error message!
0
HonorGodSoftware EngineerCommented:
Or, better yet, in the except clause, immediately after the "except", add the following statements:

  Type, value = sys.exc_info()[ : 2 ]
  print 'Exception type :', str( Type )
  print 'Exception value:', str( value )

Open in new window

0
Timothy GoldenWeb DevAuthor Commented:
now i have this:
AttributeError: 'str' object has no attribute 'format'
0
HonorGodSoftware EngineerCommented:
what?!?

str() is a built in function that converts the parameter to a string.

What, exactly, do you have in the except clause?
0
Hamed ZaghaghiProgrammerCommented:
Hi,

If you want to use format you must run your script with python 3.x or 2.6 or 2.7. I think you have python 2.5.
if you don't want to change python, use % operator instead of format.

http://docs.python.org/release/3.0.1/whatsnew/2.6.html#pep-3101-advanced-string-formatting

change this line:
newmasterfilename=os.path.join(os.path.dirname(master),'master_list_emails.{0}.txt'.format(today))

to
newmasterfilename=os.path.join(os.path.dirname(master),'master_list_emails.%s.txt' % (today))
0
Timothy GoldenWeb DevAuthor Commented:
thanks @zaghaghiP i changed the line and now get the following:
<code>

  File "bounceparser.py", line 30
    newmasterfile=open(newmasterfilename,'w')
    ^
SyntaxError: invalid syntax
[root@lamp bounced]#

</code>
Line 30 =
<code>
      newmasterfile=open(newmasterfilename,'w')
</code>

py version
<code>
Python 2.4!
</code>

whole script:
<code>
#!/usr/bin/python

#Import the necessary modules for use later:
import sys, os, fileinput, glob, datetime

def emailparser(directory, master):
      #Create a list of the locations of all of the bounceback e-mails:
      dirlist=glob.glob(os.path.join(str(directory),'*.txt'))

      #Create a list of the target e-mail addresses in each bounceback
      #from the line in the bounceback that begins "To:":
      bouncers=[email[3:].strip() for email in fileinput.input(dirlist) if email[0:3]=="To:"]

      #Open the master list of e-mails and strip each e-mail
      #of newline characters:
      masterlist=[email.strip() for email in open(str(master)).readlines()]

      #Create a new master list of e-mails by removing those that are in
      #the bouncer list and adding newline characters for later file writing:
      newmasterlist=[email+"\r\n" for email in masterlist if email not in bouncers]

      #Get today's date:
      today=str(datetime.date.today())
      
      #Create a new master e-mail file name with today's date appended:
      #newmasterfilename=os.path.join(os.path.dirname(master),'master_list_emails.{0}.txt'.format(today))
newmasterfilename=os.path.join(os.path.dirname(master),'master_list_emails.%s.txt' % (today))
      
      #Open the new master file for writing:
      newmasterfile=open(newmasterfilename,'w')
      
      #Write the new master list to the file:
      newmasterfile.writelines(newmasterlist)
      
      #Close the new master file:
      newmasterfile.close()

      #Some dialogue to confirm successful execution:
      print "Old e-mail list size: ", len(masterlist)
      print "New e-mail list size: ", len(newmasterlist)
      print "E-mails pruned: ", len(masterlist)-len(newmasterlist)
      print "Location of new master list:", newmasterfilename
      
if __name__=="__main__":
      import sys, os
      emailparser(directory=str(sys.argv[1]), master=str(sys.argv[2]))
</code>
0
Hamed ZaghaghiProgrammerCommented:
Hi,
correct the indentation of this line:
newmasterfilename=os.path.join(os.path.dirname(master),'master_list_emails.%s.txt' % (today))
0
HonorGodSoftware EngineerCommented:
The comment by zaghaghi means that this error:

SyntaxError: invalid syntax

Generally refers to the fact that you have inconsistent indentation.
Remember that Python uses indentation to identify / indicate whether statements are nested, or not.

For example:

if myString.startswith( 'something' ) :
  print "something found."

Open in new window


The print statement is indented with 2 blank spaces to indicate that it is the "then" clause of the "if statement".  All subsequent statements within this same clause must be indented the same amount of whitespace.

Special note: Some text editors will, by default, use tab characters, and spaces interchangeably. You should be careful about this.  I would strongly encourage you to replace any and all tab characters with the appropriate number of blank spaces.
0
Timothy GoldenWeb DevAuthor Commented:
none of these solutions worked and i gave up trying to make it work
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Timothy GoldenWeb DevAuthor Commented:
because i could not get anything to work
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.