python - Invalid Directory Name -
i receiving error:
file "c:/users/am/pycharmprojects/booyah/first.py", line 5, in main directory = os.chdir(input('enter directory of file want read')) notadirectoryerror: [winerror 267] directory name invalid: 'c:\\users\\am\\desktop\\sad.txt'
here code:
import os def main(): answer = input('would read, write, create, or quit?') if(answer == 'read'): directory = os.chdir(input('enter directory of file want read')) w = open(directory, 'r') contents = w.read() print(contents) w.close()
the directory copy leads text file on desktop. help!
it's bit ambiguous want achieve here, here's quick example of how parse read
command, chdir
directory specified, , print out lines of file.
i have hardcoded file name in example:
import os answer = input('would read, write, create, or quit?: ') file_name = 'sad.txt' if(answer == 'read'): directory = os.chdir(input('enter directory of file want read: ')) open(file_name, 'r') fh: line in fh.readlines(): print(line)
...and here's brief example showing reading file if want user send in path includes file well:
import os answer = input('would read, write, create, or quit?: ') if(answer == 'read'): file_name = input('enter file want read: ') open(file_name, 'r') fh: line in fh.readlines(): print(line)
Comments
Post a Comment