Skip to content
Fix Code Error

Correct way to write line to file?

March 13, 2021 by Code Error
Posted By: Anonymous

I’m used to doing print >>f, "hi there"

However, it seems that print >> is getting deprecated. What is the recommended way to do the line above?

Update:
Regarding all those answers with "n"…is this universal or Unix-specific? IE, should I be doing "rn" on Windows?

Solution

This should be as simple as:

with open('somefile.txt', 'a') as the_file:
    the_file.write('Hellon')

From The Documentation:

Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single ‘n’ instead, on all platforms.

Some useful reading:

  • The with statement
  • open()
    • ‘a’ is for append, or use
    • ‘w’ to write with truncation
  • os (particularly os.linesep)
Answered By: Anonymous

Related Articles

  • What are the undocumented features and limitations…
  • Get operating system info
  • What does do?
  • don't know what went wrong nav bar with Tailwind CSS…
  • How to remove duplicate item form array of string…
  • Logging best practices
  • Using multiple IF statements in a batch file
  • What's the difference between eval, exec, and compile?
  • What are the new features in C++17?
  • Changing PowerShell's default output encoding to UTF-8
  • Why cat does not work with parameter -0 in xargs?
  • Gnuplot line types
  • How can I find the product GUID of an installed MSI setup?
  • Is it possible to apply CSS to half of a character?
  • Start redis-server with config file
  • Error: the entity type requires a primary key
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • How do I ignore files in Subversion?
  • How to use java.net.URLConnection to fire and handle…
  • Text editor to open big (giant, huge, large) text files
  • insert tables in dataframe with years from 2000 to…
  • What is the difference between Cygwin and MinGW?
  • How to paste yanked text into the Vim command line
  • What encoding/code page is cmd.exe using?
  • Xamarin 2.0 vs Appcelerator Titanium vs PhoneGap
  • Python is not calling fucntions properly
  • Iterate and sum values based on a condition in pandas
  • Pandas pivot_table: filter on aggregate function
  • Can't install via pip because of egg_info error
  • Check synchronously if file/directory exists in Node.js
  • Null pointer exception error as method parameter
  • How do you run a command for each line of a file?
  • PHP recursive function not allowing page to load
  • WAVE file unexpected behaviour
  • What is your most productive shortcut with Vim?
  • How to get a list of sub-folders and their files,…
  • How to replace (or strip) an extension from a…
  • VBA replace a string EXCEL 2019
  • Writing a dict to txt file and reading it back?
  • InfluxDB - ERR_EMPTY_RESPONSE - Used to come up in…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • creating triggers for After Insert, After Update and…
  • Show Console in Windows Application?
  • Can Windows Containers be hosted on linux?
  • scp files from local to remote machine error: no…
  • Android Gradle plugin 0.7.0: "duplicate files during…
  • ComboBox.SelectedItem giving Null value
  • How do i update a javascript variable as its value changes?
  • Scraping Tables in Python Using Beautiful Soup…
  • Accessing an SQLite Database in Swift
  • Error in MySQL when setting default value for DATE…
  • GCC C++ Linker errors: Undefined reference to…
  • Fix top buttons on scroll of list below
  • Removing double quotes from variables in batch file…
  • How to use html template with vue.js
  • What does "Fatal error: Unexpectedly found nil while…
  • Smart way to truncate long strings
  • Fastest way to iterate over all the chars in a String
  • How to create a signed APK file using Cordova…
  • $location / switching between html5 and hashbang…
  • CMD: Export all the screen content to a text file
  • How are zlib, gzip and zip related? What do they…
  • Spark (Scala) Turn a list with duplicates into a map…
  • Find values in TXT file and replace with values from…
  • java.sql.SQLException: - ORA-01000: maximum open…
  • Backbone Collection.fetch gives me Uncaught…
  • Qt 5.1.1: Application failed to start because…
  • How can I extract embedded fonts from a PDF as valid…
  • How do I expand the output display to see more…
  • Simplest way to create Unix-like continuous pipeline…
  • How to use Servlets and Ajax?
  • How do I echo and send console output to a file in a…
  • Change multiple classes on various elements in Svelte
  • SQL find sum of entries by date including previous date
  • python requests file upload
  • update file based on a key in C
  • How to loop over files in directory and change path…
  • Can someone explain how to implement the jQuery File…
  • How can I pass a wct test while rearranging children…
  • Carriage ReturnLine feed in Java
  • Edit a specific Line of a Text File in C#
  • multiple login routes using ember-cli-simple-auth
  • jQuery .each() index?
  • Vue.js v-for loop from an object key and nested array
  • Nuxt build not working with connected other domains…
  • Typescript export vs. default export
  • Getting started with Haskell
  • What do 'real', 'user' and 'sys' mean in the output…
  • How to bring back "Browser mode" in IE11?
  • How to detect my browser version and operating…
  • Why is the DATA in SMTP not null terminated?
  • IOException: The process cannot access the file…
  • How to use requirements.txt to install all…
  • What is a 'workspace' in Visual Studio Code?
  • Memcached vs. Redis?
  • How is the 'use strict' statement interpreted in Node.js?
  • Problems with local variable scope. How to solve it?
  • How does Java resolve a relative path in new File()?
  • creating a python 2 player game with functions & classes
  • How do I install PIL/Pillow for Python 3.6?

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:

What is the difference between public, protected, package-private and private in Java?

Next Post:

How do I read / convert an InputStream into a String in Java?

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