On Linux at least, there’s no difference as far as I can tell. Here’s a test script
f1 = open('f1', 'r+') f2 = open('f2', 'rw+') f3 = open('f3', 'w+')
and its corresponding OS syscalls (using strace); tested on python 2.7.9.
open("f1", O_RDWR|O_LARGEFILE) = 3 open("f2", O_RDWR|O_LARGEFILE) = 4 open("f3", O_RDWR|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 5
See http://man7.org/linux/man-pages/man2/open.2.html for more details on the file access and creation flags.
It’s not accurate to say a file object opened with ‘r+’ cannot be used to truncate a file – it just doesn’t do so at the point the file is opened.
Related Posts:
- How to open a file using the open with statement
- write() versus writelines() and concatenated strings
- How do you use subprocess.check_output() in Python?
- How can I concatenate str and int objects?
- How do you use subprocess.check_output() in Python?
- How to return dictionary keys as a list in Python?
- What is an easy way to implement fprintf in python?
- Use Gif Logo For Loading Screen In Kivy
- How can I make a time delay in Python? [duplicate]
- Python – TypeError: ‘int’ object is not iterable
- Correct way to write line to file?
- Python – TypeError: ‘int’ object is not iterable
- Relative imports – ModuleNotFoundError: No module named x
- Best way to convert string to bytes in Python 3?
- PermissionError: [Errno 13] Permission denied
- What is setup.py?
- Invalid character in identifier
- Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory
- How to remove EOFError: EOF when reading a line?
- TypeError: cannot unpack non-iterable NoneType object
- Is there a ‘foreach’ function in Python 3?
- Python inline if statement
- Convert bytes to a string
- How to copy a dictionary and only edit the copy
- How to print like printf in Python3?
- Could not find a version that satisfies the requirement tensorflow
- Writing a list to a file with Python
- TypeError: ‘type’ object is not subscriptable when indexing in to a dictionary
- Invalid character in identifier
- ‘str’ object has no attribute ‘decode’. Python 3 error?
- How to find all the indexes of a recurring item in a list?
- (Help) TypeError: ‘str’ object cannot be interpreted as an integer
- How do order of operations go on Python?
- Why KeyError: 0
- Are dictionaries ordered in Python 3.6+?
- Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)
- builtins.TypeError: must be str, not bytes
- Pinging servers in Python
- TypeError: write() argument must be str, not bytes (Python 3 vs Python 2 )
- Import error: No module name urllib2
- takes 1 positional argument but 2 were given
- Python 3 ImportError: No module named ‘ConfigParser’
- Why am I getting this syntax error: keyword can’t be an expression
- re.search().TypeError: cannot use a string pattern on a bytes-like object
- Add Legend to Seaborn point plot
- ctypes – Beginner
- How can I install Python’s pip3 on my Mac?
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- Get ZeroDivisionError: float division in python
- What is the Python 3 equivalent of “python -m SimpleHTTPServer”
- What does -> mean in Python function definitions?
- How to delete last item in list?
- Don’t understand this SyntaxError: illegal target for annotation
- Using Queue in python
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- “for line in…” results in UnicodeDecodeError: ‘utf-8’ codec can’t decode byte
- SyntaxError invalid token
- How do I format a string using a dictionary in python-3.x?
- For loop and ‘numpy.float64’ object is not iterable error
- How to troubleshoot an “AttributeError: __exit__” in multiproccesing in Python?
- TypeError: cannot unpack non-iterable NoneType object
- python mpl_toolkits installation issue
- Shebang doesn’t work with python3
- UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xa0′ in position 20: ordinal not in range(128)
- TypeError: ‘dict_keys’ object does not support indexing
- How to strip all whitespace from string
- Python Binomial Coefficient
- pythonw.exe or python.exe?
- How to convert ‘binary string’ to normal string in Python3?
- python3 TypeError: ‘function’ object is not iterable
- How can I convert a .py to .exe for Python?
- Directing print output to a .txt file
- How to start from second index for for-loop
- Converting a sentence to piglatin in Python
- Python Error – TypeError: input expected at most 1 arguments, got 3 [duplicate]
- A general tree implementation?
- ModuleNotFoundError: No module named ‘seaborn’ in Python IDE
- Is there a ceiling equivalent of // operator in Python?
- ImportError: No module named ‘django.core.urlresolvers’
- TypeError: ‘set’ object does not support indexing
- Python 3: Multiply a vector by a matrix without NumPy
- How to fix “Can’t find a default Python” error
- TypeError: ‘newline’ is an invalid keyword argument for this function
- Root mean square of a function in python
- Print empty line?
- Checking to see if a string is an integer or float
- pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding’ when trying to install new packages
- How to know/change current directory in Python shell?
- How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?
- How to use cmp() in Python 3?
- Generator expression must be parenthesized if not sole argument
- Does python have header files like C/C++?
- Error: No module named ‘fcntl’
- Python can’t find file
- TypeError: got multiple values for argument
- IOError: [Errno 32] Broken pipe when piping: `prog.py | othercmd`
- Accessing dict_keys element by index in Python3
- Python webbrowser.open() to open Chrome browser
- Find all files in a directory with extension .txt in Python
- How to read a CSV file from a URL with Python?