cannot concatenate str and file objects : Python error

2024/11/17 13:48:07

I have

Following piece of code:

for src_filename, src_code in src_dict.iteritems(): 
try:
set.dependencies = subprocess.check_output('unifdef -s /home/c/maindir/folder/' +src_filename, shell=True)
except subprocess.CalledProcessError, e:print "code is bad" +set.propertyset.bad = 1raise
set.dependencies = list(set(set.dependencies.splitlines()))

I wanted to un-hardcode the path so I wrote following piece of code:

filepath = os.path.join(maindirpath, "folder/")

maindir is argument here: /home/c/maindir

 path = open(filepath)set.dependencies = subprocess.check_output("unifdef" '-s' path +src_filename, shell=True)

It throws following error:

TypeError: cannot concatenate 'str' and 'file' objects

I am new to python. can anybody help, where I am getting wrong?

Answer
for src_file, src_code in src_dict.iteritems():# assuming, here, that you want to honor the handle's path if already givenfilename = src_file.nameif 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!

https://en.xdnf.cn/q/120187.html

Related Q&A

Run python script from html button submit

i have a code input data to txt file :<form action="proses.php" method="post">Nomor Polisi : <br><input type="text" name="nopol"><br><…

Reading log files in python

I have a log file (it is named data.log) containing data that I would like to read and manipulate. The file is structured as follows:#Comment line 1 #Comment line 2 1.00000000,3.02502604,343260.6865…

Return more than one value in python function [duplicate]

This question already has answers here:How can I use `return` to get back multiple values from a loop? Can I put them in a list?(2 answers)Closed 1 year ago.I was trying to use *args with a for loop …

Python: Making a class to use complex numbers

I am currently trying to write a program that deals with complex numbers. I have to use classes and methods. I am trying to be able to add, subtract, multiply etc., complex numbers, as well as compare …

Return does not return anything in Spyder. It works fine with other IDE

I just moved to spyder for Python and the return function doesnt seem to work: def test():return 2 test()The IPython console is empty. If I use print instead of return it works fine. Any idea? I use p…

Error in goto module [Python]

Ok, let me start by saying I know that it is bad that I am using the goto module and I shouldnt be and blah blah blah. However, for this specific purpose I need it. Let me also say that I am new to Pyt…

How to scrape all product review from lazada in python

i currently working on web scraping of data from the lazada site using selenium in python: https://www.lazada.sg/products/loreal-paris-uv-perfect-even-complexion-sunscreen-spf50pa-30ml-i214861100-s325…

How to compare 2 successive row values in a resultset object using python

I have a table issue_logs:id | issue_id | from_status | to_status | up_date | remarks ----+----------+-------------+-----------+----------------------------------+----------…

Getting all possible combination for [1,0] with length 3 [0,0,0] to [1,1,1]

from itertools import combinationsdef n_length_combo(arr, n):# using set to deal# with duplicates return list(combinations(arr, n))# Driver Function if __name__ == "__main__":arr = 01n = 3pri…

Compare values under multiple conditions of one column in Python

I have the following data:data = {"index": [1, 2, 3, 4, 5],"name": ["A", "A", "B", "B", "B"],"type": [s1, s2, s1, s2, s3]…