🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Python Tutorials: Files Operations


Python File Operations


Python – Working with files

Python File Operations: Modes


Python File Operations: Writing a file

Python File Operations: CRUD


# Create a file
# f = open("live/createfile.txt", "x")
# Create a file called "myfile.txt":
f = open("myfile.txt", "x")
# Open a File on the Server
f = open("demofile.txt", "r")
print(f.read())
# Open a file on a different location:
f = open("D:\\myfiles\welcome.txt", "r")
print(f.read())
# Create a new file if it does not exist:
f = open("myfile.txt", "w")
# Open the file "demofile2.txt" and append content to the file:
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
# open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
# Open the file "demofile3.txt" and overwrite the content:
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
# open and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())
"""
# Overwriting a file with a content
f = open('live/createfile.txt', 'w')
f.write('Overwrite existing data.')
f.flush()
"""
# Append a file with a content
f = open('live/createfile.txt', 'a')
f.write('\nAppend this text.')
f.flush()
f.close()
# read a file with a content
f = open('live/createfile.txt')
print(f.read())
os.remove('live/createfile.txt')
{
"name": "Jason",
"hobbies": ["music", "programming", "games"],
"job": "Software Developer"
}
import json
import os
cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd) # Get all the files in that directoryclea
print("Files in %r: %s" % (cwd, files))
if os.path.isfile('sample-programs/system/input.json'):
print('The file does exist.')
else:
print('The file does not exist.')
with open('sample-programs/system/input.json', 'r') as input:
obj = json.load(input)
with open('sample-programs/system/output.txt', 'w') as output:
output.write(obj['name'] + "'s Hobbies:\n")
for hobby in obj['hobbies']:
output.write(hobby + "\n")
view raw input.py hosted with ❤ by GitHub
"""
# File Exist or not
# File Ops
Reading
Writing
Appending
Deleting
"""
import json
import os
# f = open('myfile.txt')
cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd) # Get all the files in that directoryclea
print("Files in %r: %s" % (cwd, files))
if os.path.isfile('live/myfile.txt'):
print('The file does exist.')
else:
print('The file does not exist.')
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x