Questions tagged [with-statement]

A number of languages have With statements. The Python with statement creates a new context, with associated context manager. When the context (a code block) is exited again, the context manager is notified. Please use "common-table-expression" for the SQL WITH construction.

A number of languages have With statements. Python's with statement creates a runtime context, defined by a . As the code block under the with statement is entered, a __enter__ hook is called on the context manager, and as it is exited (by any means, including exceptions and return statements), the __exit__ hook is called.

Python provides several standard context managers. File objects, for example, can be opened as a context manager, and on exit the file is automatically closed.

Context managers were defined in PEP 343.

Please use for the SQL WITH statement

1119 questions
-1
votes
1 answer

Why does the second line in "with open(file.txt)" statement return an empty string in python?

I am learning how to open text files in python and I was trying the following code: from pathlib import Path file_path = Path(r'path\to\file.txt') with open(file_path) as file_object: for line in file_object: print(line) contents =…
houss
  • 7
  • 4
-1
votes
1 answer

Can I use WITH clause inside of CAST() within a SELECT statement in Oracle?

I have code to create a view, for one column (TERMINAL_DEGREE) I am trying to cast the value of a query which contains a WITH clause. Any help is greatly appreciated. I get the following error: [Error] Execution (306: 1): ORA-00936: missing…
Anderson
  • 117
  • 2
  • 14
-1
votes
1 answer

How to remove lines from a file starting with a specific word python3

I am doing this as an assignment. So, I need to read a file and remove lines that start with a specific word. fajl = input("File name:") rec = input("Word:") def delete_lines(fajl, rec): with open(fajl) as file: text =…
Matija
  • 69
  • 8
-1
votes
1 answer

How to write every 3rd character from one file to a different file python3

This is part of an assignment. I want to read the input file with ulaz variable and write every 3rd character from that file to the output file. I was thinking about writing the first file to a list and than removing the '\n' firstly. After that,…
Matija
  • 69
  • 8
-1
votes
3 answers

Writing headers to CSV only once whilst scraping Python3

So, I am doing a course on Python3 and in the scraping section, we have an assignment to scrape the http://quotes.toscrape.com/ website and get the text, author, and link of author's bio for all of the quotes, including the ones on "next" pages. I…
-1
votes
1 answer

How to Wrap a Python 'With' Function and Reuse It Somewhere Else

Basically I have a Python With function that does a site login. Everything else I do needs to fall under a With function, so I can continue having the site connection. As you guessed, I have multiple functions that needing to start with a With…
toadead
  • 1,038
  • 16
  • 29
-1
votes
1 answer

For loop terminates after first item in python

I am trying to process multiple images in a directory by providing a with statement and a text file listing the file paths for the files I want processed (processing includes grayscaling shown and some de-noising and pixel intensity measures). With…
B. Leo
  • 3
  • 2
-1
votes
3 answers

Retrieving the code inside a 'with' statement

INGORE THIS QUESTION I clearly didn't explain well at all and none of the answers seemed to be on-topic for what I had in mind. In the example shown below, I am trying to make a more concise loop where it will eventually look like l(n), l*n or…
Doot
  • 555
  • 5
  • 15
-1
votes
1 answer

with open() gives TypeError: an integer is required (got type str)

Using pickle and trying to generate classifier result in python gives me a TypeError. I understand the general concept as to why this occurs but I am not sure what is wrong with my syntax. Everything runs until I get to this part. Thank you in…
-1
votes
2 answers

Wrapping a Python function that uses with

Say have a python function foo() that uses some resource and is meant to be called as follows: with foo(x,y,z) as f: doSomething(f) So far so good. Now lets say foo takes in a complex set of arguments based on a variety of factors, and…
chessprogrammer
  • 768
  • 4
  • 15
-1
votes
2 answers

Reading data from a text file, written in a specific period of time in Python

To explain in detail, I have a text file in which I am logging some data from a varying number of process instances(i.e there could be between 4 to 16 process instances generating the logs). All the instances write into one file in the following…
Amit Yadav
  • 4,422
  • 5
  • 34
  • 79
-1
votes
1 answer

how to access object's method inside a with statement?

I am trying to create a class to be used with the 'with' statement in python 3 but inside the 'with' statement I don't have access to the object or the methods. For example, Running the following code: class Openizer: def __init__(self,…
Yekhezkel Yovel
  • 205
  • 1
  • 2
  • 10
-1
votes
3 answers

Extract a part from a file between specific lines

I would like to know, how can I extract some data from a specific range in a big data file? Is there a way to read the content beginning and ending with "buzzwords". I would like to read line per line between *NODE and ** *NODE 13021145, …
Nils
  • 409
  • 6
  • 16
-1
votes
2 answers

Writing a text file error using python 3.x "with open" function

I am writing a text file with some of the data I analyzed using python. I am running into some error messages. Below is my code. sixteen=0.1 fifteen=0.3 fourteen=-.4 fourteen_revised=1 thirteen=2 with open('TMV_AVD.txt','w') as f: f.…
user7852656
  • 675
  • 4
  • 15
  • 26
-1
votes
1 answer

Tensoflow: Calling prediction from a function returns 'RuntimeError: Attempted to use a closed Session'

I have implemented a simple MLP in tensorflow. The structure is a class NeuralNet: class NeuralNet: def __init__(self, **options): self.type = options.get('net_type') # MLP, CNN, RNN self.n_class = options.get('classes') …
ylnor
  • 4,531
  • 2
  • 22
  • 39