1

I have to reference a file that exists in a different directory. This is just a text file, not a python module. I've read countless posts, most of which are about including modules. Nothing that I read is giving me a successful answer. Out of many attempts, this is my latest:

import os
REMOTE_FILE = open(os.path.join('/Users/me/Dropbox/otherfolder', 'text.txt'), "r")
decrypted = subprocess.check_output(['openssl', 'aes-128-cbc', '-d', '-in', REMOTE_FILE, '-base64', '-pass', key]) 

The program doesn't fail on this line immediately, but when I attempt to reference this file I get:

TypeError: Can't convert '_io.TextIOWrapper' object to str implicitly

What am I doing wrong? Thanks!

10
  • Have you tried '/Users/me/Dropbox/otherfolder/'? Feb 20, 2014 at 17:18
  • 1
    How are you accessing the file? Your problem is probably there instead of in this line.
    – Zhehao Mao
    Feb 20, 2014 at 17:27
  • 1
    By using '/' at the start of your path, you are specifying an absolute path. For example, on my system, I need to specify 'C:/' since I'm working on a Windows machine. Feb 20, 2014 at 17:31
  • 1
    Wait, you normally just assign a string to REMOTE_FILE? That would be what your problem is. Right now you are passing in a file object, not a string. You should just set REMOTE_FILE to the result of os.path.join.
    – Zhehao Mao
    Feb 20, 2014 at 17:45
  • 1
    How are you accessing the file? If you need a file path combined with a file name, you only need os.path.join('/Users/me/Dropbox/otherfolder', 'text.txt') otherwise, you are referencing an open file, and NOT the file path combined with a file name. Feb 20, 2014 at 17:48

2 Answers 2

2

Use REMOTE_FILE = os.path.join('/Users/me/Dropbox/otherfolder', 'text.txt') instead to get only the file path as a string and not an file object.

1

Your REMOTE_FILE is a file object, not a string. Given your code, you probably meant to do:

import os
REMOTE_FILE = os.path.join('/Users/me/Dropbox/otherfolder', 'text.txt')
decrypted = subprocess.check_output(['openssl', 'aes-128-cbc', '-d', '-in', REMOTE_FILE, '-base64', '-pass', key]) 

Keeping REMOTE_FILE as a string, not an object.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.