For my class I have to write code that will receive a pathname and then display the file contents if its a file or display each file and its contents if a directory is the pathname.
I wrote the following code, which works fine when I test it with my own local directory. But when I run the checks on my class portal it returns an error "No such file or directory".
Can anyone help me see what's wrong with my code?
import os
def openFyle(filename):
openFile = open(filename)
return openFile.read()
def displayFiles(filename):
if os.path.isdir(filename):
os.chdir(filename)
print("Directory name:", os.getcwd())
dirLyst = os.listdir(filename)
for file in dirLyst:
if os.path.isfile(file):
return print("File name:", file, openFyle(file))
elif os.path.exists(file) and os.path.isdir(file):
os.chdir(file)
displayFiles(os.getcwd())
else:
print("File name:", filename, openFyle(filename))
def main():
#while True:
filename = input("Enter a pathname: ")
displayFiles(filename)
if __name__ == "__main__":
main()