繁体   English   中英

将数据保存到运行当前 python 代码的另一个目录

[英]Saving data to the another directory from where the current python code is running

我想知道是否有办法将数据从运行脚本的当前工作目录保存到另一个目录?

下面是根据当前工作目录中的文件生成新数据“integ = pd.DataFrame(list)”的脚本部分,我想将数据保存到应保存集成数据的另一个文件夹中。

当前工作目录为:path = 'C:/Users/sidik/Desktop/py/measurements/18082020/BS3023' 应保存集成数据的子文件夹:eval = 'C:/Users/sidik/Desktop/py/measurements /18082020/评估/BS3023'。

"""

if file[:6] == sample and file[16:18] == power:          # select sample names and power values (1 to 10) [16:18] power  10
    print(file)
    data = pd.read_csv(file, sep='.', delimiter='\t', skiprows=13, header=None)  #thousands='.'
    idx_min = abs(data[0] - min).values.argmin()  # find the index of max and min values
    idx_max = abs(data[0] - max).values.argmin()
    integration = data.iloc[idx_max:idx_min, 1].sum().astype(str)
    list.append(integration)
    # make a text file in the folder
    integ = pd.DataFrame(list)
    integ.to_csv('Integration_'+ sample+'_power_' + power +'.txt',index=None, header=False)
    print(integration)
    # plot 
    arr_x = np.array(data[0])
    arr_y = np.array(data[1])
    array_x = np.append(array_x, [arr_x], axis=0)
    array_y = np.append(array_y, [arr_y], axis=0)

"""

可能是:

integ.to_csv(f'path/to/folder/Integration_{sample}_power_{power}.txt', index=None, header=False)

只需指定完整路径:

integ.to_csv('C:/Users/example/dir/path/' + 'Integration_'+ sample+'_power_' + power +'.txt',index=None, header=False)

暂无
暂无

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

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