Skip to content
Fix Code Error

open() in Python does not create a file if it doesn’t exist

March 13, 2021 by Code Error
Posted By: Anonymous

What is the best way to open a file as read/write if it exists, or if it does not, then create it and open it as read/write? From what I read, file = open('myfile.dat', 'rw') should do this, right?

It is not working for me (Python 2.6.2) and I’m wondering if it is a version problem, or not supposed to work like that or what.

The bottom line is, I just need a solution for the problem. I am curious about the other stuff, but all I need is a nice way to do the opening part.

The enclosing directory was writeable by user and group, not other (I’m on a Linux system… so permissions 775 in other words), and the exact error was:

IOError: no such file or directory.

Solution

You should use open with the w+ mode:

file = open('myfile.dat', 'w+')
Answered By: Anonymous

Related Articles

  • Display rows with one or more NaN values in pandas dataframe
  • How to update a rating sequentially?
  • Can't install via pip because of egg_info error
  • insert tables in dataframe with years from 2000 to…
  • How to show title in hover - css / jquery
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • How to update values in a specific row in a Python…
  • Attempting to adapt existing code that opens Excel…
  • How can I use something similar to Offset.Select and…
  • Secure FTP using Windows batch script
  • Read Numeric Data from a Text File in C++
  • Javascript text animation not triggering
  • How to use bookmark to document the edited contents…
  • How can I add error bars to geom_col in ggplot2?
  • How to change the color of vaadin-select-text-field…
  • Remove specific commit
  • Maven2: Missing artifact but jars are in place
  • Custom property on File instance not cloned when…
  • Pointer to incomplete class type is not allowed
  • What are the undocumented features and limitations…
  • Relative imports for the billionth time
  • How do you remove columns from a data.frame?
  • Creating a for loop for summing columns values in a…
  • Matching in awk
  • Formula in R not updating properly in loop
  • SVG Mask is "bleeding" on canvas edges
  • counting the number of lines in a text file
  • Error setting alias in PowerShell to Java platform
  • How to import multiple csv files into querie…
  • How to write a large buffer into a binary file in C++, fast?
  • Filter multiple values on a string column in dplyr
  • Check synchronously if file/directory exists in Node.js
  • How to Update Database from Assets Folder in App
  • How to convert string in to date in Python
  • What's the difference between eval, exec, and compile?
  • Applescript doesn't have write permission to file
  • R - recursively create dataframe columns via index…
  • How to avoid using Select in Excel VBA
  • Jenkins fails on empty grep result
  • What is your most productive shortcut with Vim?
  • Checkout another branch when there are uncommitted…
  • Smart way to truncate long strings
  • How to update Python?
  • custom text position on ipywidgets button
  • gnuplot plotting multiple line graphs
  • OOP can't get value from a class
  • Spring data jpa- No bean named…
  • Does bootstrap have builtin padding and margin classes?
  • dplyr change many data types
  • Shiny App Dashboard - 'Clear' button to remove imported file
  • How to remove rows where all columns are zero using…
  • SQL query return data from multiple tables
  • Pandas / Python - Compare numerically each row with…
  • Why does C++ code for testing the Collatz conjecture…
  • discord.ext.commands.errors.CommandInvokeError:…
  • Having trouble with my nav bar/header, It used to…
  • Spring Boot - Cannot determine embedded database…
  • Azure Sql : Unable to Replace HTML String
  • Git status shows files as changed even though…
  • Tomcat 7 "SEVERE: A child container failed during start"
  • How can you find and replace text in a file using…
  • Scanning numbers from a file line to line and…
  • How to apply rolling t.test with pandas?
  • How to prevent scrolling the whole page?
  • Increase number of axis ticks
  • Editing specific line in text file in Python
  • How does PHP 'foreach' actually work?
  • import dat file into R
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • dplyr mutate with conditional values
  • C++ template,typename and operator
  • Allow only pdf, doc, docx format for file upload?
  • "Thinking in AngularJS" if I have a jQuery background?
  • The definitive guide to form-based website authentication
  • Active tab issue on page load HTML
  • Maven Could not resolve dependencies, artifacts…
  • Reading binary file and looping over each byte
  • How to download and save an image in Android
  • Specifying java version in maven - differences…
  • The superclass "javax.servlet.http.HttpServlet" was…
  • Determine if two rectangles overlap each other?
  • Obtain most recent value for based on index in a…
  • Best way to replace multiple characters in a string?
  • Different ways of loading a file as an InputStream
  • Python - IOError: [Errno 13] Permission denied:
  • How do you read a Python Traceback error?
  • Loading existing .html file with android WebView
  • Tuning hyperparameters in mlr does not produce…
  • Which maven dependencies to include for spring 3.0?
  • data.table vs dplyr: can one do something well the…
  • Standardize data columns in R
  • Initialize a struct from elements of a vector
  • How can I write a function to detect whether a…
  • Convert True/False value read from file to boolean
  • How do I merge two dictionaries in a single…
  • Convert a normal python code to an MPI code
  • Resolving require paths with webpack
  • Create a file if one doesn't exist - C
  • How do I test to see if a line of a .txt exists or…
  • Flask, how to correctly upload CSV?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Python `if x is not None` or `if not x is None`?

Next Post:

Using Python 3 in virtualenv

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error