Python Forum
problems with python script and special characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problems with python script and special characters
#1
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:])
Reply


Messages In This Thread
problems with python script and special characters - by last08 - Mar-29-2019, 12:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 477 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Copy xml content from webpage and save to locally without special characters Nik1811 14 1,059 Mar-26-2024, 09:28 AM
Last Post: Nik1811
Question Special Characters read-write Prisonfeed 1 657 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,391 Jun-29-2023, 11:57 AM
Last Post: gologica
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,760 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,515 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  Extract continuous numeric characters from a string in Python Robotguy 2 2,674 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,877 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,978 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,334 May-28-2020, 05:27 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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