Some beginner's questions about python

nbaztec

Master KOD3R
Hey did i combine these the two lines like the writer wanted?
Code:
input = open(from_file)
indata = input.read()

to
indata = open(from_file).read()
I think yes, but the correct way ought to be
Code:
     indata = (input=open(from_file)).read();
This way you get the file in input so you can close it later.


also it gave an error while closing the file, saying something like that nothing is opened. Can someone explain what the line does?
Make sure the file exists.

Also what's the need to close the files? To free up RAM? don't the files get closed when the script ends? So, it's only useful when opening big or many files?
Free RAM - not exactly. Whenever you open a file you effectively own a resource - A resource that you should release when you are done with it. In scripts/programs you might need to close that file so that other parts of your code (or other programs) can use it since you have acquired a lock to that resource. Also, if you open a file in "write" mode, close() flushes out all buffers in the RAM (since you're actually working on the file in a memory buffer) and begins write to the disk (in simple terms saving your work back to disk).
 
OP
Niilesh

Niilesh

Padawan
I think yes, but the correct way ought to be
Code:
     indata = (input=open(from_file)).read();
This way you get the file in input so you can close it later.
OK,But i my way is better ;)
Your last line made me realise my mistake

Make sure the file exists.
I was confused. when i give this command - "indata.close()" it gives an error that string object cannot be closed! So it automatically closed it after getting the data?
Also how do you close to_file after this? - "open(to_file, 'w').write(indata)"

Free RAM - not exactly. Whenever you open a file you effectively own a resource - A resource that you should release when you are done with it. In scripts/programs you might need to close that file so that other parts of your code (or other programs) can use it since you have acquired a lock to that resource. Also, if you open a file in "write" mode, close() flushes out all buffers in the RAM (since you're actually working on the file in a memory buffer) and begins write to the disk (in simple terms saving your work back to disk).
So without closing it you cannot write?

Hey I was able to strip down the code into 1 line
Code:
open(raw_input('output? ' ), 'w').write(open(raw_input('input? ')).read())
Now the problem is it asks for output before input. Anyway to get the correct order?
 

nbaztec

Master KOD3R
I was confused. when i give this command - "indata.close()" it gives an error that string object cannot be closed! So it automatically closed it after getting the data?
Also how do you close to_file after this? - "open(to_file, 'w').write(indata)"
You don't close indata. You close input - the file handle. And no you cannot use open(...).write(...).close() since open(), not write(), returns the file handle.


OK,But i my way is better ;)
There's no my way or your way. It's the correct way and yours is not it.


Hey I was able to strip down the code into 1 line
Code:
open(raw_input('output? ' ), 'w').write(open(raw_input('input? ')).read())
Now the problem is it asks for output before input. Anyway to get the correct order?
One day you'll realize how bad it is NOT to close an underlying stream, release an allocated buffer, dispose an object, close an established connection, heck! even force gc() a managed object. One-liners (think Perl), however, pretty they may be must not come at the cost of writing bad code. Till then check the following code:
Code:
f = open('foo', 'w')
f.write('hi');
g = open('foo', 'w')
g.write('hello')
The output I get is
Code:
hillo
And kindly explain to yourself why this is happening. And I'm not even getting on the multi-threaded part yet.


But since you like hacky one-liners anyway here's your solution.
Code:
[open(raw_input('output? ' ), 'w').write(l) for l in open(raw_input('input? '), 'r')]

Bad code is bad code. No excuses. Period.
 
Last edited:

Liverpool_fan

Sami Hyypiä, LFC legend
Note: In Python the file is closed when the last reference to the file object is garbage collected.

This will do for ya one line code: (Hacky as hell, but whatever, tested in Py3)
PHP:
inp = open(input('input? ')).read() and open(input('output? '), 'w').write(inp)
 

nbaztec

Master KOD3R
Note: In Python the file is closed when the last reference to the file object is garbage collected.

If there's one thing I've learnt it's - never trust the garbage collector. There's no guarantee when it will be invoked. It's there for our convenience not so that we become lazy programmers.
 

Liverpool_fan

Sami Hyypiä, LFC legend
If there's one thing I've learnt it's - never trust the garbage collector. There's no guarantee when it will be invoked. It's there for our convenience not so that we become lazy programmers.

Well you have to trust the garbage collector sometimes as well, after all it lays the foundation of the language. In CPython implementation, the file is closed while the object is garbage collected. In fact by using a file object with "with", python programmers are encouraged to let the file object deference outside its scope and not to explicitly close the file object. Indeed explicitely closing the file randomly can generate plenty of problems. (What if you close the file twice, etc.)

PHP:
with open(filename, mode) as file_object:
   # Your code here
   pass
See you don't explictly close the file here and let the GC do the job. (All right "with" does the job here for you as well but still).

You are correct not to trust the GC when you there is no specification and guarantee how a GC will behave, in those circumstances assumptions lead to programmers shooting themselves on the foot. But otherwise with clear cuts specs and implementation, it's fine.
 

nbaztec

Master KOD3R
Well you have to trust the garbage collector sometimes as well, after all it lays the foundation of the language. In CPython implementation, the file is closed while the object is garbage collected. In fact by using a file object with "with", python programmers are encouraged to let the file object deference outside its scope and not to explicitly close the file object. Indeed explicitely closing the file randomly can generate plenty of problems. (What if you close the file twice, etc.)

PHP:
with open(filename, mode) as file_object:
   # Your code here
   pass
See you don't explictly close the file here and let the GC do the job. (All right "with" does the job here for you as well but still).
with is semantically similar to using in C# and both essentially work in the same way. While using requires the enclosed object to implement the IDispose interface [method: void Dispose()], with requires the object to provide __enter__() and __exit__() (same principle). Both are built into their native languages to call the appropriate methods when leaving the block/scope. There's no question of relying on gc here, since with/using do the job by cleaning up in __exit__()/Dispose(). Agreeably, this is the best way to use handles in both Python & C#, or for that matter, any object implementing these methods/interface. But for others, you gotta do it old skool. ;)

You are correct not to trust the GC when you there is no specification and guarantee how a GC will behave, in those circumstances assumptions lead to programmers shooting themselves on the foot. But otherwise with clear cuts specs and implementation, it's fine.
Exactly my point. If you know what you're doing and what is actually required, you won't be having any issues.
 

©mß

Journeyman
So,I am learning Python(v2.7.4) in school.
python.PNG
So while I was practicing,I thought if i could write the line "print Input1+Input2"
along with the line "print Answer is".
I tried to do it so but it gave a syntax error.
So, can lines be written as I am trying to write?

Also, tell me how to upload image as OP did.
I want the image to appear rather than the link.
 
Top Bottom