Writing out to a directory in python

In summary: Which is why it was giving me the error.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.
  • #1
dacruick
1,042
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
  • #2
os.chdir(path) changes the dir. readlines() will read all the lines in a file.
 
  • #3
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:
  • #4
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
 
  • #5
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.
 
  • #6
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
 

FAQ: Writing out to a directory in python

1. How do I write out to a directory in python?

The most common way to write out to a directory in python is by using the open() function. You can specify the directory path and the file name, and then use the write() function to add content to the file.

2. Can I create a new directory using python?

Yes, you can use the os.makedirs() function to create a new directory in python. You can specify the directory path and any necessary permissions.

3. How can I check if a directory exists in python?

The os.path.exists() function can be used to check if a directory exists in python. It will return True if the directory exists, and False if it does not.

4. How do I append to a file in a specific directory in python?

You can use the open() function with the a mode to append to a file in a specific directory in python. This will add new content to the end of the file without overwriting existing content.

5. Is it possible to write out to a directory in python without using the open() function?

Yes, there are alternative methods to write out to a directory in python, such as using the shutil.copyfile() function to copy a file to a new location, or the os.rename() function to move a file to a different directory.

Similar threads

Replies
16
Views
3K
Replies
8
Views
1K
Replies
9
Views
1K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
2
Views
1K
Back
Top