Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

book titles with invalid characters for filename under windows arise error #7

Open
jackjackk opened this issue Sep 3, 2016 · 0 comments

Comments

@jackjackk
Copy link

jackjackk commented Sep 3, 2016

I have the same problem described here
http://stackoverflow.com/questions/22620965/ioerror-errno-22-invalid-mode-wb-or-filename

I was wondering whether the filename could be normalized, at least under windows, before creating the file with the filename of the title.

For the moment I'm importing unicodedata and using the following verbose code (and not immune to errors, as I allow the \ to be in the filename, as it is used for referring to the partial path) in export_txt to make things work:

        try:
            with open(filename, 'wb') as f:
                f.write("\n\n---\n\n".join(lines))
        except:
            oldfilename = filename
            filename = unicodedata.normalize('NFKD', filename).encode('ascii','ignore')
            valid_chars = '\\-_.() abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
            filename = ''.join(c for c in filename if c in valid_chars)
            print 'Converting', oldfilename, 'to', filename
            with open(filename, 'wb') as f:
                f.write("\n\n---\n\n".join(lines))

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant