with open("file.txt", "w") as file:
for line in ["hello", "world"]:
file.write(line)
# Basic syntax:
file_object = open("filename", "mode")
file_object.write("Data to be written")
file_object.close()
# Example usage:
file_object = open("/path/to/my_filename.txt", "w") # w = write, r = read
file_object.write("Line of text to write")
file_object.close()
# read, write, close a file
# catch error if raise
try:
file = open("tryCatchFile.txt","w")
file.write("Hello World")
file = open("tryCatchFile.txt", "r")
print(file.read())
except Exception as e:
print(e)
finally:
file.close()
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())
#1) To type a string using the keyboard module:
#pip install keyboard
import keyboard
string = "This is what I typed"
keyboard.write(string)
#2) To check if an object is of the type 'str' (to check if the object is a string):
if type(object) == str:
#3) To print a string:
string = "This is displayed in your Big Black Console"
print(string)