473,412 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

with statement for two files

Can open two files in a with statement:

with open(src) as readin, open(dst,"w") as writin: # WRONG: comma
doesn't work
...

-- so that you have transactional safety for two file descriptors?
The comma syntax doesn't work, but is there a way, except for

with open(src) as readin:
with open(dst,"w) as writin:
...

Cheers,
Alexy
Jul 27 '08 #1
4 1165
braver schrieb:
Can open two files in a with statement:

with open(src) as readin, open(dst,"w") as writin: # WRONG: comma
doesn't work
...

-- so that you have transactional safety for two file descriptors?
The comma syntax doesn't work, but is there a way, except for

with open(src) as readin:
with open(dst,"w) as writin:
...
I'm not aware of any pre-defined context manager which does that.
But you can write your own context manager to do that, even a
generalized combinator for taking two managers and create one, like this:

with context_creator(open, [src], open, [dst, "w"]) as readin, writin:
...

A fundamental problem though would be that the semantics become
difficult. If closing the "outer" file fails, it's impossible to
"rollback" the inner one.

So I think it really is better to use the nested with, which makes it
crystal clear that the inner block might succeed independently from the
outer one.

Diez
Jul 27 '08 #2
braver <de*********@gmail.comwrites:
with open(src) as readin, open(dst,"w") as writin: # WRONG: comma
doesn't work
...
-- so that you have transactional safety for two file descriptors?
use contextlib.nexted().
Jul 28 '08 #3
Paul Rubin wrote:
braver <de*********@gmail.comwrites:
>with open(src) as readin, open(dst,"w") as writin: # WRONG: comma
doesn't work
...
-- so that you have transactional safety for two file descriptors?

use contextlib.nexted().
You mean contextlib.nested I guess. Didn't know about that module, cool!

However, the fundamental problem stays: rolling back only works if the
innermost context fails.

Diez
Jul 28 '08 #4
Diez B. Roggisch wrote:
Paul Rubin wrote:
use contextlib.nexted().

You mean contextlib.nested I guess.
Although "nexted" is an intriguing-sounding word. I wonder
what it could mean?

--
Greg
Jul 29 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bart Nessux | last post by:
Should an if statement have a corresponding else statement? Or, is it OK to have an if statement by itself. For completeness, it seems the two should be together, but from experience I know that a...
1
by: Kevin Potter | last post by:
We have an application that has been running on IIS4 and IIS5 for quite some time, without problem We're now migrating to IIS6 (windows/2003), and have run into a what might? be a Javascipt...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
5
by: Sam | last post by:
Hi, I have one table like : MyTable {field1, field2, startdate, enddate} I want to have the count of field1 between startdate and enddate, and the count of field2 where field2 = 1 between...
1
by: somnamblst | last post by:
I have a Flash form that uses ASP to write to Access db. I have an HTML form that uses ASP to upload files and email those files and sends copies to recipients based on the form fields. I would like...
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
2
jwwicks
by: jwwicks | last post by:
C/C++ Programs and Debugging in Linux This tutorial will give you a basic idea how to debug a program in Linux using GDB. As you are aware Visual Studio doesn’t run on Linux so you have to use...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.