简体   繁体   English

Python将txt文件保存在另一个文件夹中

[英]Python save txt file in a different folder

the following script is running well and saving the txt output in the Desktop as I am running the script from Desktop. 当我从桌面运行脚本时,以下脚本运行良好,并将txt输出保存在桌面中。 However, I want to save the txt files to my Documents in a new folder named ASCII. 但是,我想将txt文件保存到名为ASCII的新文件夹中的文档中。 How can I give the command for doing that. 我该如何给出命令。 The 8phases.txt has the following lines- 8phases.txt具有以下几行:

-1  1   -1

-1  1   1

1   1   1

1   -1  1

-1  -1  -1

1   1   -1

1   -1  -1

-1  -1  1

The script- 剧本-

import numpy as np
import matplotlib.pyplot as plt

D=12
n=np.arange(1,4)
x = np.linspace(-D/2,D/2, 3000)
I = np.array([125,300,75])
phase = np.genfromtxt('8phases.txt')

I_phase = I*phase

for count,i in enumerate(I_phase):
    F = sum(m*np.cos(2*np.pi*l*x/D) for m,l in zip(i,n))
    s = np.column_stack([x,F])
    np.savetxt((str(count)+'.txt'),s)

Any help please- 任何帮助请-

您可能应该在savetxt方法的参数中提供完整路径,例如:

np.savetxt(r"C:\ASCII\%s.txt" % count,s)

You can try the following 您可以尝试以下

from os.path import osp
userdoc = osp.join(osp.expanduser("~"),'Documents')
np.savetxt(osp.join(userdoc, "%s.txt" % count),s)

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

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