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

cannot use bytes as filename #383

Open
petaflot opened this issue Jun 13, 2024 · 3 comments
Open

cannot use bytes as filename #383

petaflot opened this issue Jun 13, 2024 · 3 comments

Comments

@petaflot
Copy link

  File "[...]/pylatex/base_classes/latex_object.py", line 168, in generate_tex
    with open(filepath + '.tex', 'w', encoding='utf-8') as newf:
              ~~~~~~~~~^~~~~~~~
TypeError: can't concat str to bytes

I suppose this says it all..

@SG420
Copy link

SG420 commented Oct 4, 2024

This isn't a pylatex issue, you're trying to concatanate filepath, which presumably isn't a string, to '.tex', which is a string. You need to first cast filepath to a str

@petaflot
Copy link
Author

petaflot commented Oct 4, 2024

I disagree. not all bytestrings can be casted to str. some filesystems (ie. xfs, zfs) accept bytes that cannot be encoded in human-readable strings (ie. ascii, utf)

the code should read sth like

try:
    with open(filepath + '.tex', 'w', encoding='utf-8') as newf:
       ...
except TypeError:
    with open(filepath + b'.tex', 'w', encoding='utf-8') as newf:
       ...

@petaflot
Copy link
Author

petaflot commented Oct 4, 2024

better still:

try:
    full_filename = filepath + '.tex'
except TypeError:
    full_filename = filepath + b'.tex'
with open(full_filename, 'w', encoding='utf-8') as newf:
    ...

you get the point. one can also check on the type of filepath. however you prefer, there are a number of somewhat equivalent way to do it.

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

2 participants