This is exactly what bytearray
is for:
newFileByteArray = bytearray(newFileBytes) newFile.write(newFileByteArray)
If you’re using Python 3.x, you can use bytes
instead (and probably ought to, as it signals your intention better). But in Python 2.x, that won’t work, because bytes
is just an alias for str
. As usual, showing with the interactive interpreter is easier than explaining with text, so let me just do that.
Python 3.x:
>>> bytearray(newFileBytes) bytearray(b'{\x03\xff\x00d') >>> bytes(newFileBytes) b'{\x03\xff\x00d'
Python 2.x:
>>> bytearray(newFileBytes) bytearray(b'{\x03\xff\x00d') >>> bytes(newFileBytes) '[123, 3, 255, 0, 100]'
Related Posts:
- How can I install pip on Windows?
- Best way to convert string to bytes in Python 3?
- Does Python have a string ‘contains’ substring method?
- Difference between import numpy and import numpy as np
- No module named ‘sklearn.cross_validation’
- Maximum and Minimum values for ints
- FileNotFoundError: [Errno 2] No such file or directory [duplicate]
- python capitalize first letter only
- Is there a short contains function for lists?
- ImportError: No module named pandas. Pandas installed pip
- How to Execute a Python Script in Notepad++?
- What does the Pydoc module do?
- size of NumPy array
- Best way to strip punctuation from a string
- How can I explicitly free memory in Python?
- filename.whl is not supported wheel on this platform
- What is the meaning of [:] in python [duplicate]
- Finding and replacing elements in a list
- socket.error: [Errno 48] Address already in use
- No module named MySQLdb
- TypeError: Missing 1 required positional argument: ‘self’
- Convert a tensor to numpy array in Tensorflow?
- How to import the class within the same directory or sub directory?
- Why do I get AttributeError: ‘NoneType’ object has no attribute ‘something’?
- What is __pycache__?
- Permission denied error by installing matplotlib
- Python equivalent of a given wget command
- How to find list intersection?
- Error while Executing jupyter notebook: [Errno 2] No such file or directory
- ValueError: max() arg is an empty sequence
- How do I insert a JPEG image into a python Tkinter window?
- How to remove punctuation marks from a string in Python 3.x using .translate()?
- Calculating Covariance with Python and Numpy
- What is the easiest way to remove all packages installed by pip?
- In practice, what are the main uses for the new “yield from” syntax in Python 3.3?
- Subplot for seaborn boxplot
- Dump a NumPy array into a csv file
- How to delete a character from a string using Python
- What is the difference between sparse_categorical_crossentropy and categorical_crossentropy?
- Inverse of a matrix using numpy
- NameError: name ‘self’ is not defined
- gaierror: [Errno 8] nodename nor servname provided, or not known (with macOS Sierra)
- How to print colored text to the terminal
- TypeError: can only concatenate list (not “str”) to list
- What’s the idiomatic syntax for prepending to a short python list?
- What is the difference between “is None” and “== None”
- How do I check if a list is empty?
- IndexError: list index out of range and python
- Don’t understand why UnboundLocalError occurs (closure) [duplicate]
- OSError – Errno 13 Permission denied
- ImportError: No module named ‘Queue’
- ImportError: No Module Named bs4 (BeautifulSoup)
- How to calculate rolling / moving average using python + NumPy / SciPy?
- How to fix “Attempted relative import in non-package” even with __init__.py
- Errno 13 Permission denied Python
- How to get all possible combinations of a list’s elements?
- Converting strings to floats in a DataFrame
- Pylint “unresolved import” error in Visual Studio Code
- Errno 13 Permission denied Python
- ValueError: object too deep for desired array while using convolution
- module has no attribute
- How to print number with commas as thousands separators?
- double for loops in python
- Convert hex string to int in Python
- Function to close the window in Tkinter
- Is there a way to specify which pytest tests to run from a file?
- Python function overloading
- Breaking out of nested loops
- Youtube_dl : ERROR : YouTube said: Unable to extract video data
- How to stop a function
- What is the common header format of Python files?
- How to clear up screen in pygame?
- Efficiently sorting a numpy array in descending order?
- How to replace multiple substrings of a string?
- How to use cookies in Python Requests
- Move column by name to front of table in pandas
- How to search through dictionaries?
- Element-wise addition of 2 lists?
- Converting integer to binary in python
- Open File in Another Directory (Python)
- Python: Making a beep noise
- Negation in Python
- Checking if a string can be converted to float in Python
- Python conversion from binary string to hexadecimal
- ERROR: Could not build wheels for cryptography which use PEP 517 and cannot be installed directly
- ImportError: No module named Cython.Distutils
- Python Brute Force algorithm
- What’s the correct way to convert bytes to a hex string in Python 3?
- How can I install a .egg Python package on Windows (attempt using easy_install not working)
- Case insensitive regular expression without re.compile?
- replace characters not working in python [duplicate]
- ElementClickInterceptedException: element click intercepted:
- During handling of the above exception, another exception occurred
- Python Remove last char from string and return it
- Python 3 integer division [duplicate]
- numpy/scipy/ipython:Failed to interpret file as a pickle
- What is the Python equivalent of static variables inside a function?
- Python – difference between two strings
- Flask-framework: MVC pattern
- Installing win32gui python module