Linked Questions
46 questions linked to/from How can I open multiple files using "with open" in Python?
6 votes
2 answers
1k views
Opening two files simultaneously on a "with" context [duplicate]
I'm wondering if there is a construction in Python 3 that allows me to open two (or more) files on the same with context. What I'm looking for is something like this: from pathlib import Path file1 ...
0 votes
1 answer
2k views
Python: Context manager with multiple objects? [duplicate]
Is it possible to use the with context manager syntax with more than one object in Python? For example, something like: with open("file1.txt") as file1, open("file2.txt") as file2: ...
2 votes
0 answers
2k views
Python: Opening multiple files using 'with open' [duplicate]
Possible Duplicate: Python: open multiple files using “with open”? Does, With open() not works with python 2.6 I have seen other answers to help me with this. But I can't seem to get any of the ...
-1 votes
2 answers
772 views
Python: open multiple txt file in a directory [duplicate]
Hi i was going to open multiple txt files in a directory. But i get error message of File "testTopic.py", line 9, in <module> with open(path +i, 'r') as f: IOError: [Errno 2] No ...
0 votes
0 answers
93 views
Conditional with statement with multiple variables in python [duplicate]
I would like to accept a certain number (limited to - say, 3) to evaluate a condition within the a with statement. I did find it's possible to do something like this. Can I do something similar to the ...
655 votes
8 answers
252k views
Multiple variables in a 'with' statement?
Is it possible to declare more than one variable using a with statement in Python? Something like: from __future__ import with_statement with open("out.txt","wt"), open("in.txt") as file_out, ...
113 votes
7 answers
114k views
How to do unit testing of functions writing files using Python's 'unittest'
I have a Python function that writes an output file to disk. I want to write a unit test for it using Python's unittest module. How should I assert equality of files? I would like to get an error if ...
7 votes
2 answers
29k views
How to open multiple files in loop, in python
I'm new to python. I'm want to open multiple files in Python. I'm can open each of them with open() function. I'm not sure about formatting. with open("/test/filename.css", "r") as f: s = f....
3 votes
2 answers
5k views
How to open concurrently two files with same name and different extension in python?
I have a folder with multiple couple of files: a.txt a.json b.txt b.json and so on: Using a for loop i want to open a couple of file (a.txt and a.json) concurrently. Is there a way to do ...
2 votes
1 answer
4k views
Python Performance Tuning: JSON to CSV, big file
I've been asked by a colleague to convert 6 huge files from the "Yelp Dataset Challenge" from somewhat "flat," regular JSON into CSV (he thinks they look like fun teaching data). I thought I could ...
0 votes
2 answers
2k views
Taking each item in a list and writing them on a text file
I have a list of numbers and need to put them all into a text file on their own line. I can't figure out how to call to each item and print them. I only know how to write strings into a text file. ...
2 votes
2 answers
2k views
Nested with blocks in Python, level of nesting variable
I would like to combine columns of various csv files into one csv file, with a new heading, concatenated horizontally. I want to only select certain columns,chosen by heading. There are different ...
1 vote
1 answer
2k views
Python File I/O: invalid syntax?
I'm trying to open 2 files, one of which has content and one which is empty. For the lines that are not headers, I read each line and process it before writing the processed line to the empty file, ...
0 votes
1 answer
3k views
Why am I getting "[Errno 13] Permission denied:" when making consecutive open and write calls?
Update It's throwing the error with a direct call now, and in the relations docs. I got rid of write_el() altogether and just do this: ... if el["doc_type"] == "node": with ...
1 vote
2 answers
255 views
How can I write out to a file in a specific order?
I want to read in from a file, "ee_pinkH1.xpk" This is what my ee_pinkH1.xpk file looks like: label dataset sw sf 1H 1H_2 NOESY_F1eF2e.nv 4807.69238281 4803.07373047 600.402832031 600.402832031 1H.L ...