Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write PID to file in Python?

Tags:

python

pid

crud

I get the current running script PID by os.getpid(), and try to store it in a file but I get an error that write() only accepts 'string' and not 'int'. So how to pass PID as a string to write()?

My code:

import os

outputFile = open('test.txt', "w")
pid = os.getpid()
outputFile.write(pid)
outputFile.close()
like image 290
Milad Abooali Avatar asked Nov 22 '25 19:11

Milad Abooali


1 Answers

I have to use type convertor str() like this:

import os

with open('test.txt', 'w', encoding='utf-8') as f:
    f.write(str(os.getpid()))
like image 51
Milad Abooali Avatar answered Nov 24 '25 08:11

Milad Abooali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!