{"action":"created","issue":{"url":"https://api.github.com/repos/mstamy2/PyPDF2/issues/191","labels_url":"https://api.github.com/repos/mstamy2/PyPDF2/issues/191/labels{/name}","comments_url":"https://api.github.com/repos/mstamy2/PyPDF2/issues/191/comments","events_url":"https://api.github.com/repos/mstamy2/PyPDF2/issues/191/events","html_url":"https://github.com/mstamy2/PyPDF2/issues/191","id":65476599,"number":191,"title":"AttributeError: 'unicode' object has no attribute 'write' when passing in a unicode filename to merger.write","user":{"login":"troyshu","id":1057286,"avatar_url":"https://avatars.githubusercontent.com/u/1057286?v=3","gravatar_id":"","url":"https://api.github.com/users/troyshu","html_url":"https://github.com/troyshu","followers_url":"https://api.github.com/users/troyshu/followers","following_url":"https://api.github.com/users/troyshu/following{/other_user}","gists_url":"https://api.github.com/users/troyshu/gists{/gist_id}","starred_url":"https://api.github.com/users/troyshu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/troyshu/subscriptions","organizations_url":"https://api.github.com/users/troyshu/orgs","repos_url":"https://api.github.com/users/troyshu/repos","events_url":"https://api.github.com/users/troyshu/events{/privacy}","received_events_url":"https://api.github.com/users/troyshu/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-03-31T15:20:26Z","updated_at":"2015-08-18T18:53:26Z","closed_at":null,"body":"Not sure where else this occurs, but noticed that a PdfFileMerger can't handle the case where a unicode filename string is passed into merge(). E.g this fails (passing in u\"test.pdf\"), throwing a ```AttributeError: 'unicode' object has no attribute 'write'```:\r\n\r\n```python\r\nfrom PyPDF2 import PdfFileMerger, PdfFileReader\r\nmerger = PdfFileMerger(strict=False)\r\nfor filename in filenames:\r\n    merger.append(PdfFileReader(file(filename, 'rb')))\r\n\r\nmerger.write(u\"test.pdf\")\r\n```\r\n\r\nWhile this succeeds:\r\n\r\n```python\r\nfrom PyPDF2 import PdfFileMerger, PdfFileReader\r\nmerger = PdfFileMerger(strict=False)\r\nfor filename in filenames:\r\n    merger.append(PdfFileReader(file(filename, 'rb')))\r\n\r\nmerger.write(\"test.pdf\")\r\n```\r\n\r\nDoesn't seem like expected behavior?"},"comment":{"url":"https://api.github.com/repos/mstamy2/PyPDF2/issues/comments/132317386","html_url":"https://github.com/mstamy2/PyPDF2/issues/191#issuecomment-132317386","issue_url":"https://api.github.com/repos/mstamy2/PyPDF2/issues/191","id":132317386,"user":{"login":"dhudson1","id":13439475,"avatar_url":"https://avatars.githubusercontent.com/u/13439475?v=3","gravatar_id":"","url":"https://api.github.com/users/dhudson1","html_url":"https://github.com/dhudson1","followers_url":"https://api.github.com/users/dhudson1/followers","following_url":"https://api.github.com/users/dhudson1/following{/other_user}","gists_url":"https://api.github.com/users/dhudson1/gists{/gist_id}","starred_url":"https://api.github.com/users/dhudson1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dhudson1/subscriptions","organizations_url":"https://api.github.com/users/dhudson1/orgs","repos_url":"https://api.github.com/users/dhudson1/repos","events_url":"https://api.github.com/users/dhudson1/events{/privacy}","received_events_url":"https://api.github.com/users/dhudson1/received_events","type":"User","site_admin":false},"created_at":"2015-08-18T18:53:26Z","updated_at":"2015-08-18T18:53:26Z","body":"The reason that this occurs is that Unicode strings (type '```unicode```') aren't really Python's default string (type '```str```').\r\n\r\nTo confirm this, simply plug and chug the following lines:\r\n```python\r\nunicode_string = u'Hello, world!'\r\nascii_string = 'Hello, world!'\r\nprint(type(ascii_string))\r\nprint(type(unicode_string))\r\n```\r\n\r\nThis issue is only with Python 2.X. Since I use Python 3.X, I was unable to test this, or several of the following lines of code.\r\n\r\nIf you really need to pass the Unicode string into the merger's write function and not an already opened file object, you could just make a little Unicode to ASCII conversion if statement within the ```write``` method. An example:\r\n```python\r\nif isinstance(fileobj, unicode):\r\n     fileobj = fileobj.encode('utf8')\r\n```\r\n\r\nIf you do decide to go with the above example, insert those lines of code between the initial definition of ```my_file``` and the ```if``` statement immediately following, and it should work like expected, but only if your file names follow the UTF-8 encoding scheme.\r\n\r\nThe reason for this exception is that PyPDF2 assumes that you pass it either an instance of the ```str``` class or an instance of the ```file``` class. Since an instance of the ```unicode``` class is neither of those and since PyPDF2 assumes that ```fileobj``` is a file if not a string, PyPDF2 calls the ```write``` function, thinking that ```fileobj``` is an instance of the ```file``` class, which leads to that particular error you were experiencing."}}