3

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()
2
  • outputFile.write(str(pid)) Commented Feb 25, 2021 at 11:50
  • 1
    You can convert it to string with str. Also, you may consider working with files with with key-word. geeksforgeeks.org/with-statement-in-python Commented Feb 25, 2021 at 11:51

1 Answer 1

5

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()))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.