Skip to content Skip to sidebar Skip to footer

How To Open Files In Pycharm

as you can see from the title I am having difficulties opening files in pycharm. Every time I try to open a file it says: 'Errno2: no such file or directory' I already put the file

Solution 1:

First, check your current working directory:

import osprintos.path.abspath(os.curdir)

If it is not the path of your base project directory, then do:

os.chdir('/path/baseproject/directory/or/where/fileislocated')

Solution 2:

@Dani jel Solution below works for me. The easiest way how to do it is to use absolute file path and you have to use raw string as a file path with r in front of string or use double slashes or forward slashes.

import os
print(os.path.abspath(os.curdir)) # it´s different than file path, so I used the absolute file path in method open

myfile = open(r"C:\Users\Ghost\Downloads\special.txt", "r")
# or relative file path# myfile = open(r".\Downloads\special.txt", "r")print(myfile.read())
HELLO WORLD! # it´s from file

Post a Comment for "How To Open Files In Pycharm"