Python Writing out to a directory in python

  • Thread starter Thread starter dacruick
  • Start date Start date
  • Tags Tags
    Python Writing
AI Thread Summary
To change the directory where files are created in a Python program, the os.chdir(path) function can be used. Additionally, new directories can be created with os.mkdir(newdir). For the file handling process, instead of reading and writing line by line, it's more efficient to read all lines into a list using readlines() and then write them out in one go. The newtxt.writelines() function is recommended for writing multiple lines at once, as it avoids issues with out-of-range errors that can occur when using newtxt.write() with a list. To format the output correctly, joining the lines with "\n" can ensure proper line breaks are maintained when writing to the new file.
dacruick
Messages
1,038
Reaction score
1
Hi, I have a program which is reading in line by line information, saving that, and writing it out to a file. How do I change where the files are created? Also, can I create a folder to write the files out to in my program?

The second issue I have is this. right now my program opens the file, reads a line, writes a line, reads a line, writes a line, until its finished. I want it to read all of the lines, then open the file, then write all of them. Would I save all my lines to an array then fout.write(array[0:x],'w')??
 
Last edited:
Technology news on Phys.org
os.chdir(path) changes the dir. readlines() will read all the lines in a file.
 
dacruick said:
Also, can I create a folder to write the files out to in my program?
os.mkdir(newdir)

Would I save all my lines to an array then fout.write(array[0:x],'w')??
Think like a python person: lists, dude, lists.
You can do the readlines in a list iteration:
newtxt = open(newtext, 'w')
words = [readlines(file) for file in file_list]
text = " ".join(words)
newtxt.write(text)
 
Last edited:
I had the list and it wasnt working. But i figured it out. apparently the newtxt.write function will not work for multiple lines. I had a list of lines from 0 - x and i was trying to use newtxt.write(list[0:x]) and it was giving me some stupid out of range errors. But I got it and if you wanted to know its the newtxt.writelines function. but thanks
 
dacruick said:
But I got it and if you wanted to know its the newtxt.writelines function. but thanks
Thanks. The whole point of using the join is to get around the weird list things. You effectively turn the text into one long string that you're writing out. A "\n".join(text) may work for creating new lines.
 
yeah the endline wasnt an issue here. It seems that the line that i read out of a file included some form of endline so that when i saved them to my list it saved the endline too
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top