简体   繁体   English

python3 - os.path更改

[英]python3 - os.path changes

I am using python 3.3 in Windows 7. 我在Windows 7中使用python 3.3

The python file, main.py is in D:\\my proj\\$MY\\1 python文件main.py位于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: 所以,当我在cmd中运行我的程序时,它给我输出如下:

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. 现在, usage.txt文件在我的main.py所在的目录中生成,意味着在1文件夹中。

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 . 但是我试图修改它应该保存在report文件夹中的路径,因为你可以看到os.path.join的值。

So, I want to save the generated usage.txt in the report folder instead of 1 . 所以,我想将生成的usage.txt保存在report文件夹中而不是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")

由于p5是报告目录的路径,因此将在该目录中打开usage.txt文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM