Home > Mobile >  How to create .txt file with value and current path where this file is save in Python?
How to create .txt file with value and current path where this file is save in Python?

Time:06-01

I try to create .txt file in Python I have code like below:

with open('readme.txt', 'w') as f:
    f.write("XYZ")

And as a result I have .txt file with value: XYZ

But I need to create .txt file with values like below, so XYZ and path whete this file is created:

XYZ
Path: "C:\Users\John\Desktop\projects\readme.txt"

How can I modify my code where I create this file so as to have as a result values presented above?

CodePudding user response:

you need the cwd

import os
cwd = os.getcwd()

with open('readme.txt', 'w') as f:
    f.write("XYZ")
    f.write('\nPath: "' os.getcwd() '\\readme.txt"')
  • Related