Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python write to text file with new line

# This will open a new file called myFile.txt and 
# write 0 - 4 using a loop, each on a new line.
# Note the "
" part!

with open("myFile.txt", "w") as file:
  for i in range(0,5):
    file.write(str(i) + "
")

# We do not need to use "file.close()" because 
# we used "with open(...)" which is considered 
# to be more "pythonic"
Comment

python read text file next line

filepath = 'Iliad.txt'
with open(filepath) as fp:
   line = fp.readline()
   cnt = 1
   while line:
       print("Line {}: {}".format(cnt, line.strip()))
       line = fp.readline()
       cnt += 1
Comment

python write text file on the next line

file = open("sample.txt", "w")
file.write("Hello
")
file.write("World
")
file.close()
Comment

PREVIOUS NEXT
Code Example
Python :: Python function to compute factorial of a number. 
Python :: sort dictionary by value and then key python 
Python :: python count variable and put the count in a column of data frame 
Python :: requests save data to disk 
Python :: types of dict comprehension 
Python :: python extract zip file 
Python :: python - count total numeber of row in a dataframe 
Python :: how to change case of string in python 
Python :: increase axis ticks pyplot 
Python :: python run command 
Python :: float infinity python 
Python :: install a lower version of python using conda 
Python :: difference between set and tuple in python 
Python :: django migrate model 
Python :: cv2 copy image 
Python :: python multiplication array 
Python :: how to create a variable in python 
Python :: python check if dataframe series contains string 
Python :: pyspark dataframe to parquet 
Python :: django view - APIView (retrieve, update or delete - GET, PUT, DELETE) 
Python :: python find digits in string 
Python :: how to get length of string in python 
Python :: Write a Python program to sum all the items in a dictionary. 
Python :: Compute the 2d histogram of x and y. 
Python :: dijkstras python 
Python :: how to play and stop music python 
Python :: use python dotenv 
Python :: python filter dict 
Python :: python check if list 
Python :: python cholesky 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =