About 978,000 results
Open links in new tab
  1. python - Correct way to write line to file? - Stack Overflow

    May 28, 2011 · the_file.write('Hello\n') 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 …

  2. Writing string to a file on a new line every time - Stack Overflow

    May 27, 2010 · I want to append a newline to my string every time I call file.write(). What's the easiest way to do this in Python?

  3. Writing a list to a file with Python, with newlines

    508 What are you going to do with the file? Does this file exist for humans, or other programs with clear interoperability requirements? If you are just trying to serialize a list to disk for later use by the same …

  4. python - How do I append to a file? - Stack Overflow

    Jan 16, 2011 · On some operating systems, opening the file with 'a' guarantees that all your following writes will be appended atomically to the end of the file (even as the file grows by other writes). A few …

  5. python - How to write a list of numbers as bytes to a binary file ...

    With Python 2 on Windows, I found that writing a bytearray still converts \n to \r\n, making it unsatisfactory for binary data, if the "b" flag is not passed when opening the file.

  6. python - How do I write JSON data to a file? - Stack Overflow

    How do I write JSON data stored in the dictionary data to a file? f = open ('data.json', 'wb') f.write (data) This gives the error: TypeError: must be string or buffer, not dict

  7. python - Directing print output to a .txt file - Stack Overflow

    Is there a way to save all of the print output to a txt file in python? Lets say I have the these two lines in my code and I want to save the print output to a file named output.txt.

  8. How can I use pickle to save a dict (or any other Python object)?

    I have looked through the information that the Python documentation for pickle gives, but I'm still a little confused. What would be some sample code that would write a new file and then use pickle...

  9. python - Write a file to a directory that doesn't exist - Stack Overflow

    How do I using with open() as f: ... to write the file in a directory that doesn't exist. For example:

  10. python - How to replace/overwrite file contents instead of appending ...

    When you say "replace the old content that's in the file with the new content", you need to read in and transform the current contents data = file.read(). You don't mean "blindly overwrite it without needing …