Cannot Concatenate 'str' And 'file' Objects : Python Error
Solution 1:
for src_file, src_code in src_dict.iteritems():
# assuming, here, that you want to honor the handle's path if already given
filename = src_file.name
if not '/' in filename:
filename = os.path.join(filepath, filename)
try:
set.dependencies = subprocess.check_output(['unifdef', '-s', filename])
except subprocess.CalledProcessError:
pass # etc.
By the way, set
is a bad variable name, since set
is also a Python data type; you're making the data type unusable in your code by shadowing it with a like-named variable. Don't do that!
Solution 2:
I think I understand, here is what I would try:
import os
D = os.getcwd() # returns a string
newD = (D + '/folder/') # concatenates the two strings together
I use this all the time, if os.getcwd() (get current working directory) isn't exactly what you are looking for, there are similar options under the os module that will also return a string. As I am sure you know, typing help(os) in IDLE should show you all the options.
Solution 3:
You are trying to concatenate 'unifdef -s /home/c/maindir/folder/'
with src_filename
The first is a string, so the second is likely a file object.
As per your edited version, you probably need
bytestr = subprocess.check_output( [ 'unifdef', '-s', filepath + os.path.basename(src_filename.name) ] )
instead of
set.dependencies = subprocess.check_output("unifdef" '-s' path +src_filename, shell=True)
(remember that subprocess.check_output
returns a byte string).
Then assign bytestr
to whatever you need.
You might even want
try :
bytestr = subprocess.check_output( [ 'unifdef', '-s', filepath + os.path.basename(src_filename.name) ], stderr=subprocess.STDOUT )
except subprocess.CalledProcessError :
...
This may work, although it is quite rudimentary, and you probably have much better options workable with the rest of your code (which we do not know).
Post a Comment for "Cannot Concatenate 'str' And 'file' Objects : Python Error"