Writing and Reading Files in Os
Writing and Reading Files in Os
import datetime
def file_date(filename):
# Create the file in the current directory
with open (filename,'w') as file:
pass
timestamp = os.path.getmtime(filename)
c=datetime.datetime.fromtimestamp(timestamp)
# Convert the timestamp into a readable format, then into a string
print(file_date("newfile.txt"))
# Should be today's date in the format of yyyy-mm-dd
import os
def new_directory(directory, filename):
# Before creating a new directory, check to see if it already exists
if os.path.isdir(directory) == False:
os.mkdir(directory)
print(new_directory("PythonPrograms", "script.py"))
import os
def parent_directory():
print(parent_directory())