Hi,

I was wondering if there was a way to set a directory in python code when I read/write files. For example, if I have

import scipy

def test_io():
    scipyArray1 = scipy.array([[1.0,2.0],[3.0,4.0],[5.0,6.0]])
    outFile = file('tmpdata1.txt', 'w')
    scipy.io.write_array(outFile,scipyArray1)
    outFile.close()
    inFile = file('tmpdata1.txt', 'r')
    scipyArray2 = scipy.io.read_array(inFile)
    print 'type(scipyArray2):', type(scipyArray2)
    print 'scipyArray2:\n', scipyArray2
    
test_io()

the program proceeds to make a file at a "random" location. Can I make it read/write at a location that I set it to? I would like to read data file from a certain location.

Recommended Answers

All 2 Replies

f = open("C:\\My_Directory", "w") # change the directory in the 1st argument
f.write(data)

import os
os.chdir(path) --> change directory to the one in path

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.