As background, I have created a list with elements of different filenames with their full path
fileList = ['/../filea.dat', '/../fileb.dat']
for i, elem in enumerate(fileList):
hexed = fileList[i]
subprocess.Popen("hexdump " + hexed + " > hexed.dat", shell=True)
with open("hexed.dat", "r") as f:
for line in f:
if "statement" in line:
value = "examplevalue"
if value == "examplevalue"
other subprocess statements that create a file that will again be used later
TypeError: cannot concatenate 'str' and 'list' objects
You are close. You got the type error because
Popen requires that you also set
shell=True when passing in a string instead of a list. But there is another problem:
Popen doesn't wait for the process to complete so when you read the file, there isn't anything useful in it yet. A different strategy is to skip file redirection and read the output stream directly. Note also that you don't need to use
enumerate... the for loop gets the values in the list already. And we can skip the shell and pass the command as a list.
for hexed in fileList: proc = subprocess.Popen(["hexdump", hexed], stdout=subprocess.PIPE, stderr=open(os.devnull, 'w')) for line in proc.stdout: if "statement" in line: value = "examplevalue" proc.wait() if proc.returncode != 0: print('error') # need less lame error handling! if value == "examplevalue" other subprocess statements that create a file that will again be