문제

I am using python 3.3 in Windows 7.

The python file, main.py is in D:\my proj\$MY\1

I wrote the following code in that file:

import os

file_usage = "usage.txt"
p1 = os.getcwd()
print ("os.getcwd(): ", p1)

p5 = os.path.join(p1,"report")
print ("os.path.join: ", p5)

file = open(file_usage, "a")
file.write ("*****BLAH-BLAH*****")
file.close()

So, When I run my program in cmd, it gives me output like:

os.getcwd():  D:\my proj\$MY\1
os.path.join:  D:\my proj\$MY\1\report

Now, usage.txt file is generating in the directory where my main.py is located means inside the 1 folder.

But I have tried to modify the path that it should be saved in report folder as you can see the value of os.path.join.

So, I want to save the generated usage.txt in the report folder instead of 1. What type of changes should I do? or what other modules do I need to use?

I'm a novice user. So, please try to tell me in-depth or send me links from where I can get more knowledge or examples related to this. I'm awaiting for your reply.

도움이 되었습니까?

해결책

open(os.path.join(p5, "usage.txt"), "w")

Since p5 is the path to the report directory, this will open a usage.txt file inside that dir.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top