Skip to content

cannot use bytes as filename #383

Open

Description

@petaflot
  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..

Activity

  1. SG420 commented on Oct 4, 2024

    @SG420

    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

  2. petaflot commented on Oct 4, 2024

    @petaflot
    Author

    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:
           ...
    
  3. petaflot commented on Oct 4, 2024

    @petaflot
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions