Writing out to a directory in python

  • Context: Python 
  • Thread starter Thread starter dacruick
  • Start date Start date
  • Tags Tags
    Python Writing
Click For Summary

Discussion Overview

The discussion revolves around file handling in Python, specifically focusing on changing the directory for file creation, creating folders, and writing multiple lines to a file. Participants explore methods for reading lines from a file, storing them, and subsequently writing them to another file.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant inquires about changing the directory for file creation and creating a folder within the program.
  • Another participant suggests using os.chdir(path) to change the directory and mentions readlines() for reading all lines from a file.
  • Creating a new directory can be accomplished with os.mkdir(newdir), as noted by a participant.
  • There is a discussion about storing lines in a list and the appropriate method for writing them to a file, with one participant suggesting newtxt.write(array[0:x],'w').
  • A participant shares their experience with an error when using newtxt.write(list[0:x]), discovering that newtxt.writelines() is the correct method for writing multiple lines.
  • Another participant emphasizes the use of "\n".join(text) to concatenate lines into a single string for writing, addressing issues with line endings.
  • One participant notes that the lines read from the file may already include newline characters, which affected how they were stored in the list.

Areas of Agreement / Disagreement

Participants generally share insights and solutions, but there are varying approaches to handling lists and writing to files, indicating that multiple views remain on the best practices for these tasks.

Contextual Notes

Some limitations include potential misunderstandings about list indexing and the behavior of different file writing methods in Python. The discussion does not resolve these issues fully, as participants share experiences rather than definitive solutions.

Who May Find This Useful

This discussion may be useful for Python programmers looking to understand file handling, directory management, and effective ways to write multiple lines to files.

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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
35
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K