Is there anyway I could write to tempfile and include it in a command, and then close/remove it. I would like to execute the command, eg: some_command /tmp/some-temp-file.
Many thanks in advance.
import tempfile temp = tempfile.TemporaryFile() temp.write('Some data') command=(some_command temp.name) temp.close()
generate, produce, design, make, fabricate, fashion, manufacture, build, construct, erect, do, turn out. bring into being, originate, invent, initiate, engender, devise, frame, develop, shape, form, mould, forge, concoct, hatch. informal knock together, knock up, knock off.
Definition of creat (Entry 1 of 2) : an East Indian herb (Andrographis paniculata) having a juice that is a strong bitter tonic variously used in local medicine. creat- combining form. variants: or creato-
"to bring into being," early 15c., from Latin creatus, past participle of creare "to make, bring forth, produce, procreate, beget, cause," related to Ceres and to crescere "arise, be born, increase, grow," from PIE root *ker- (2) "to grow." De Vaan writes that the original meaning of creare "was 'to make grow', which ...
create. verb. cre·ate | \ krē-ˈāt \ created; creating.
Complete example.
import tempfile with tempfile.NamedTemporaryFile() as temp: temp.write('Some data') if should_call_some_python_function_that_will_read_the_file(): temp.seek(0) some_python_function(temp) elif should_call_external_command(): temp.flush() subprocess.call(["wc", temp.name])
Update: As mentioned in the comments, this may not work in windows. Use this solution for windows
Update 2: Python3 requires that the string to be written is represented as bytes, not str, so do instead
temp.write(bytes('Some data', encoding = 'utf-8'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With