pathlib
integrates seemlessly with open
only in Python 3.6 and later. From Python 3.6’s release notes:
The built-in
open()
function has been updated to acceptos.PathLike
objects, as have all relevant functions in theos
andos.path
modules, and most other functions and classes in the standard library.
To get it to work in Python 3.5 and Python 3.6, just convert the object to a string:
contents = open(str(filename), "r").read()
Related Contents:
- How to get absolute path of a pathlib.Path object?
- Recursively iterate through all subdirectories using pathlib
- Is there a Pathlib alternate for os.path.join?
- Adding another suffix to a path that already has a suffix with pathlib
- Is there an idiomatic way to add an extension using Python’s Pathlib?
- From stat().st_mtime to datetime?
- How do I append a string to a Path in Python?
- Concatenate strings from several rows using Pandas groupby
- Reading file using relative path in python project
- Using tqdm progress bar in a while loop
- Is it necessary or useful to inherit from Python’s object in Python 3.x?
- How do I use cache_clear() on python @functools.lru_cache
- Bare asterisk in function parameters?
- Add an element in each dictionary of a list (list comprehension)
- f-strings vs str.format()
- Why is starred iterable unpacking in a return statement invalid syntax without parentheses before Python 3.8?
- How to redirect stdout and stderr to logger in Python
- When will Jython support Python 3? [closed]
- TypeError: a bytes-like object is required, not ‘str’
- How to extract text from an existing docx file using python-docx
- How to extract all the emojis from text?
- What is the difference between __init__.py and __main__.py?
- Error when creating a new text file with python?
- Python 3.7 logging: f-strings vs % [duplicate]
- Printing result of the zip() function in Python 3 gives “zip object at ” [duplicate]
- During handling of the above exception, another exception occurred
- What are assignment expressions (using the “walrus” or “:=” operator)? Why was this syntax added?
- Why do I get “TypeError: not all arguments converted during string formatting” trying to substitute a placeholder like {0} using %?
- tf.data.Dataset: how to get the dataset size (number of elements in an epoch)?
- What do underscores in a number mean? [duplicate]
- Rolling Mean on pandas on a specific column
- Unexpected ‘{‘ in field name when doing string formatting
- python mpl_toolkits installation issue
- TypeError: ‘
- How to write bytes to a file in Python 3 without knowing the encoding?
- Sort by multiple keys using different orderings [duplicate]
- How do chained assignments work?
- Invalid Token when using Octal numbers
- How to fix ” DeprecationWarning: invalid escape sequence” in Python?
- Python unittest’s assertDictContainsSubset recommended alternative [duplicate]
- Convert bytes to a string in python 3
- Using 100% of all cores with the multiprocessing module
- Check if rows in one dataframe exist in another dataframe
- How to run a Python script in a ‘.py’ file from a Google Colab notebook?
- cx_Oracle error. DPI-1047: Cannot locate a 64-bit Oracle Client library
- Why does Python 3 find this ISO8601 date: “2019-04-05T16:55:26Z” invalid?
- Python’s “open()” throws different errors for “file not found” – how to handle both exceptions?
- Python pandas: remove everything after a delimiter in a string
- ValueError: shape mismatch: objects cannot be broadcast to a single shape
- Writing Python 2.7 code that is as close to Python 3.x syntax as possible