file-writing

How do I specify new lines on Python, when writing on files?

为君一笑 提交于 2019-11-26 15:43:55
In comparison to Java (in a String), you would do something like "First Line\r\nSecond Line" . So how would you do that in Python, for purposes of writing multiple lines on a regular file? Charlie Martin It depends on how correct you want to be. \n will usually do the job. If you really want to get it right, you look up the newline character in the os package . (It's actually called linesep .) Note: when writing to files using the Python API, do not use the os.linesep . Just use \n ; Python automatically translates that to the proper newline character for your platform. python_powered The new

Writing a dictionary to a text file?

南楼画角 提交于 2019-11-26 08:04:54
问题 I have a dictionary and am trying to write it to a file. exDict = {1:1, 2:2, 3:3} with open(\'file.txt\', \'r\') as file: file.write(exDict) I then have the error file.write(exDict) TypeError: must be str, not dict So I fixed that error but another error came exDict = {111:111, 222:222} with open(\'file.txt\', \'r\') as file: file.write(str(exDict)) The error: file.write(str(exDict)) io.UnsupportedOperation: not writable I have no idea what to do as I am still a beginner at python. If anyone

How do I specify new lines on Python, when writing on files?

☆樱花仙子☆ 提交于 2019-11-26 05:57:23
问题 In comparison to Java (in a String), you would do something like \"First Line\\r\\nSecond Line\" . So how would you do that in Python, for purposes of writing multiple lines on a regular file? 回答1: It depends on how correct you want to be. \n will usually do the job. If you really want to get it right, you look up the newline character in the os package. (It's actually called linesep .) Note: when writing to files using the Python API, do not use the os.linesep . Just use \n ; Python

How to redirect 'print' output to a file using python?

半腔热情 提交于 2019-11-26 00:31:31
问题 I want to redirect the print to a .txt file using python. I have a \'for\' loop, which will \'print\' the output for each of my .bam file while I want to redirect ALL these output to one file. So I tried to put f = open(\'output.txt\',\'w\'); sys.stdout = f at the beginning of my script. However I get nothing in the .txt file. My script is: #!/usr/bin/python import os,sys import subprocess import glob from os import path f = open(\'output.txt\',\'w\') sys.stdout = f path= \'/home/xug/nearline