DirtyDaver DirtyDaver - 10 months ago 36
Python Question

accessing sub directory in python

I'm trying to access a folder that is in the current directory in which I am writing my code. the folder I am currently in is cs113

I've tried to do

file2 = open("/cs113/studentstoires/Zorine.txt", "r)"

Would someone please tell me whey this doesn't work I've also tried to write the directory name like this:

open("/studentstories/Zorine.txt", "r")

Do I need to import sys and use sys.listdir or os.path? If so can you please give a quick little example how to do this? Thank you in advance.

Answer Source

If it's in the current directory, and it's the directory where you execute the script from, you should write the path without the / up front like so:

file2 = open("studentstories/Zorine.txt", "r")