0

I'm trying to access a .txt file in my root account to open and read in python. My code looks like this:

>>> path = 'root/unpackedFiles/enrollment_fact.txt'
>>> read = open(path,'r')
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    read = open(path,'r')
FileNotFoundError: [Errno 2] No such file or directory: 'root/unpackedFiles/enrollment_fact.txt'

I've also tried several variations of this with no such luck. Including:

path = 'unpackedFiles/enrollment_fact.txt' and path = 'enrollment_fact.txt'

2
  • Can you do an ls of the file? Might be a permission problem Commented Nov 15, 2017 at 18:30
  • 3
    Full paths start with a slash, it should be /root/unpackedFiles/enrollment_fact.txt, you can test that by doing ls /root/unpackedFiles/enrollment_fact.txt on your terminal Commented Nov 15, 2017 at 18:32

2 Answers 2

2

Your path is incorrect. Absolute paths need to start with a slash for the root directory. Your path should look like this:

path = '/root/unpackedFiles/enrollment_fact.txt'
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you.
0
>>> path = 'ports.txt'                                          
>>> read = open ( path, 'r')                                    
>>> print(read)                                                 
<_io.TextIOWrapper name='ports.txt' mode='r' encoding='cp1252'> 
>>> exit()                

You have to give either relative path or absolute path.And your code you are missing / before root and so interpreter unable to find the file at given path.

Relative path:

$ ls ports.txt                                                  
ports.txt                                                       

Absolute path:

$ readlink -f ports.txt                                         
/c/Users/rgenupula/ports.txt                                    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.