4

I'm pretty new with Python and finding some issues on a trivial code. I'm using Pycharm together with Anaconda.

This is my code:

posSentences = open('rt-polarity-pos.txt', 'r')

print (posSentences.read())

There is no issue in reading the file and printing it out when running it/terminal.

But when I try to run the same command in the console I get:

FileNotFoundError: [Errno 2] No such file or directory

I so checked the directories of the console, but they seem fine and are the same of the running file:

console setup

Thank you for your help!

Tommaso Guerrini
  • 1,499
  • 5
  • 17
  • 33
  • 1
    Heya - What is your working directory when you run the script? It might be worth creating a variable that holds the Fully Qualified Path Name of the parent folder and prefixing your file with this? e.g PROJECT_ROOT = os.path.dirname(os.path.realpath(\_\_file\_\_)) – YFP Aug 26 '16 at 08:36
  • 6
    Also, when posting a code, don't post a picture. Copy the code here. – khajvah Aug 26 '16 at 08:37
  • @YFP It's the same of the console: C:Users\tomlarey\Desktop\textan. Running it works, but through the console it doesn't. Ok thank you! I'll try it, even if right now I'm in trouble, I come from R which is straightforward in comparison! khajvah Ok, I'll do it the next time since you already solved my issue! – Tommaso Guerrini Aug 26 '16 at 08:54

1 Answers1

1

Python interpreter is run from Desktop\textan but the file is in Desktop\textan\textan\ directory, so the path to the file in python code becomes textan\rt...txt

khajvah
  • 4,889
  • 9
  • 41
  • 63
  • Thank you very much! I don't understand why tough: the working directory I set for the console is the same I set when running the file. Furthermore the code that makes me print the proper output is: 'open("C:\\Users\\tomlarey\\Desktop\\textan\\rt-polarity-pos.txt",'r')' , so the full path.. I'd welcome your insight on that, thank you again! – Tommaso Guerrini Aug 26 '16 at 08:51
  • 1
    Ok, I got confused about smth. My answer is wrong. Please "unaccept" it – khajvah Aug 26 '16 at 08:55
  • I don't want to presume anything, but could it be a Pycharm issue: http://stackoverflow.com/questions/34304044/pycharm-current-working-directory?rq=1 – Tommaso Guerrini Aug 26 '16 at 08:57
  • 2
    No you don't presume anything. I know that my answer is wrong. You should accept answers in this site only when it fixed your issue. – khajvah Aug 26 '16 at 08:59