Python Forum

Full Version: problems with python script and special characters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
im new to python and am trying to use the below script but when i run it i get this error:
Error:
IOError: [Errno 2] No such file or directory: '/root/user/split_=?Utf-8?Q?Temp_Glass/Mas_1213_Tempest?=\r\n =?Utf-8?Q?Tian/Kontakt_byr=C3=A5.mbox'
The python script is attached below. it analyzes an .MBOX file and split it into smaller .MBOX files based on the label name.
I've copied the script from here: https://github.com/danburzo/gmail-mbox2maildir

I think the issue is that its trying to create a folder structure but that fails. I actually dont care about the folder structure , i just need the splitted mbox files. Anyone have any ideas on how to rewrite this?

script is here in full

#!/usr/bin/env python

# Adapted from:
# http://wboptimum.com/splitting-gmail-mbox-by-label/

import sys
import getopt
import mailbox

def main(argv):
	in_mbox = "inbox.mbox"
	prefix = "split_"
	try:
		opts, args = getopt.getopt(argv, "i:p:", ["infile=", "prefix="])
	except getopt.GetoptError:
		print("python splitgmail.py -i  -p ")
		sys.exit(2)

	for opt, arg in opts:
		if opt in ("-i", "--infile"):
			in_mbox = arg
		elif opt in ("-p", "--prefix"):
			prefix = arg
	print("Processing file - " + in_mbox + " with prefix = " + prefix)
	boxes = {"inbox": mailbox.mbox(prefix+"Inbox.mbox", None, True), "sent": mailbox.mbox(prefix+"Sent.mbox", None, True),"archive":mailbox.mbox(prefix+"Archive.mbox", None, True)}

	for message in mailbox.mbox(in_mbox):
		gmail_labels = message["X-Gmail-Labels"]       # Could possibly be None.
		if not gmail_labels:
			boxes["archive"].add(message)
			continue
		gmail_labels = gmail_labels.lower()
		if "spam" in gmail_labels:
			continue
		elif "inbox" in gmail_labels:
			boxes["inbox"].add(message)
		elif "sent" in gmail_labels:
			boxes["sent"].add(message)
		else:
			saved = False
			for label in gmail_labels.split(','):
				if label != "important" and label != "unread" and label != "starred" and label != "newsletters":
					box_name = prefix+label.title()+".mbox"
					if box_name not in boxes:
						boxes[box_name] = mailbox.mbox(box_name, None, True)
					boxes[box_name].add(message)
					saved = True
					break
			if not saved:
				boxes["archive"].add(message)

if __name__ == "__main__":
    main(sys.argv[1:])
Hi last08, welcome to the forums! Could you please post the entire error message? It shows more valueable information.

I assume as you have not written this script yourself, but found it somewhere online, now you don't want to learn anything about it, but just have somebody repair it for you? Correct me, if I'm wrong.

The way to go about it would be, contact the author. If you are familiar with Github, you can file an issue there, and state the error message.

If you would indeed like to understand the script and repair it yourself, we are ready to help you up to speed. Please do ask questions where you lack understanding.