python overwrite file – How to move and overwrite a file in Python?

python overwrite file use file.truncate() if you want to do inplace replace: Overwrite a File in Python Using the open() Function and Using the file.truncate() Method.

python write to file overwrite

file = open(pakainfo.txt”,”w”) 
 
file.write(“Welcome To Pakainfo”) 
file.write(Pakainfo website focuses on all web language and framework tutorial PHP.”) 
file.write(“Laravel, Codeigniter, Nodejs, API, MySQL, AJAX, jQuery, JavaScript, Demo.”) 
file.write(“Learn Programming Solutions For Free”) 
 
file.close() 

python overwrite file | Overwrite a File in Python

Replace and overwrite instead of appending python overwrite file Example with demo. Also learn to Python – Replace String in File, Example 1: Replace string in File, Example 2: Replace string in the same File.

Using the open() Function

Overwrite a File in Python Using the open() Function
Example code:

with open('mediaUpload/docFileMain.txt', "w") as docFileMain:
    docFileMain.write(newData)
with open('mediaUpload/docFileMain.txt', "r") as docFileMain:
    data = docFileMain.read()

with open('mediaUpload/docFileMain.txt', "w") as docFileMain:
    docFileMain.write(newData)

Using the file.truncate() Method

Example
Overwrite a File in Python Using the file.truncate() Method

with open('mediaUpload/docFileMain.txt','r+') as docFileMain:
    data = docFileMain.read()
    docFileMain.seek(0)
    docFileMain.write('newData')
    docFileMain.truncate()

Also Read: How To Convert Hex To RGB And RGB To Hex In Python?

Read and overwrite a file in Python

Example

f = open(yourflname, 'r+')
info = f.read()
info = re.sub('foobar', 'bar', info)
f.seek(0)
f.write(info)
f.truncate()
f.close()

Python – Move and overwrite files and folders

Example: Program to overwrite a folder containing a file using python.

import os
import shutil

path = 'C:/Users/PAKAINFO/Desktop/folder'

print("Before moving file:")
print(os.listdir(path))


memberinfo = 'welcome folder'
results = 'pakainfo folder'

memberinfoPath=path+'/'+memberinfo
resultsPath=path+'/'+results

if os.path.isdir(resultsPath+'/'+memberinfo):
		print(memberinfo,'exists in the results path!')
		shutil.rmtree(resultsPath+'/'+memberinfo)
		
elif os.path.isfile(resultsPath+'/'+memberinfo):
		os.remove(resultsPath+'/'+memberinfo)
		print(soucre,'deleted in',results)

res = shutil.move(memberinfoPath, resultsPath)

print("After moving file:")
print(os.listdir(path))

print(memberinfo,'has been moved!')

print("Results path:", res)

Python – How to Replace String in File?

Example 1: Replace string in File


fin = open("content.txt", "rt")

fout = open("results.txt", "wt")

for line in fin:
	
	fout.write(line.replace('pyton', 'python'))

fin.close()
fout.close()

Example 2: Replace string in the same File


fin = open("content.txt", "rt")

content = fin.read()

content = content.replace('pyton', 'python')

fin.close()

fin = open("content.txt", "wt")

fin.write(content)

fin.close()

I hope you get an idea about python overwrite file.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment