Python File Not Closing Properly [duplicate] - python

This question already has answers here:
Why does Python allow mentioning a method without calling it?
(6 answers)
Closed 4 years ago.
My file is not being closed properly and I cannot figure out why:
open_sample_file = codecs.open(ssis_txt_files_2[a], 'r', "utf-16")
whatever = open_sample_file.readlines()
open_sample_file.close
print(open_sample_file)
output:
<codecs.StreamReaderWriter object at 0x0331F3B0>
Shouldn't the output return None?

You have to call
open_sample_file.close()

For one, you need to call the close method:
open_sample_file.close()
Then, what will result is a closed file, not None.
>>> open_sample_file
<closed file 'filename', mode 'r' at 0x109a965d0>
Finally, the usual way to handle files in Python is using with-statements, which take care of closing the file for you:
with codecs.open(filename) as open_sample_file:
# do work
# the file will be closed automatically when the block is done

Related

why python read informaton in file but not it's content? [duplicate]

This question already has an answer here:
Python Read File Content [duplicate]
(1 answer)
Closed 2 years ago.
I try to Python read and then print text from file score.txt (in score.txt is text hrllo world) i write this command:
score = open("data/score.txt", "r")
print(score)
and output is:
<_io.TextIOWrapper name='data/score.txt' mode='r' encoding='cp1250'>
how can i print "hello world" from file score.txt?
You probably want to read the whole filo into the variable in your case.
score = open("data/score.txt", "r").read()
See https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
I also offer some unsolicited advice: I recommend using a so-called context manager which will automatically close the file after you're done using it (even in case reading the file fails for some reason).
with open("data/score.txt", "r") as score_file:
print(score_file.read())
This is not really very important in your case, but it is an accepted best practice and should be followed whenever possible.

How to write "subprocess.run" results to a log file without using "shell=True"? [duplicate]

This question already has answers here:
How do I redirect stdout to a file when using subprocess.call in python?
(2 answers)
Closed 2 years ago.
Currently, I am using the following format to write the run results to a log file.
p = subprocess.run(["mpiexec -n 2 ./executor >log"],shell=True)
Could anyone tell me how to avoid using the "shell=True" while I can write a log file?
Thank you.
Just split the arguments yourself, open the file yourself, and pass the open file to run to make it send the output there:
with open('log', 'wb') as outfile:
p = subprocess.run(['mpiexec', '-n', '2', './executor'], stdout=outfile)

Get system file type names python [duplicate]

This question already has answers here:
How can I check the extension of a file?
(14 answers)
How to check type of files without extensions? [duplicate]
(10 answers)
Closed 4 years ago.
Is there any way to retrieve a file type name in Python using the OS module?
An example made up command:
>>> os.file_type('txt')
Would return:
'Text Document'
Any help would be appreciated :)
Oscar.
For getting file type you need to check the extension of the file
I think this can help.
import os
if os.path.splitext(file)[1] == ".txt":
pritn 'Text Document'
For os related task you can look over this doc.
https://github.com/Projesh07/Python-basic/blob/master/python_os_module/python_os_module.py

Python - print() - debugging -show file and line number [duplicate]

This question already has answers here:
filename and line number of Python script
(11 answers)
Closed 5 years ago.
When using print() in python, is it possible to print where it was called? So the output will look like var_dump in php with xdebug. Eg. I have script D:\Something\script.py, and at line 50, there is a print("sometest"), so the output will look like this:
D:\Somethinq\script.py:50 sometest
Or is there any module that could achieve this? In large projects, it's really hard to manage where these prints came from.
So, using answers provided in filename and line number of python script , this function can be called instead of print(), and prints line number before every output:
from inspect import currentframe
def debug_print(arg):
frameinfo = currentframe()
print(frameinfo.f_back.f_lineno,":",arg)

How to check if command line argument is file or not in python? [duplicate]

This question already has answers here:
How do I check whether a file exists without exceptions?
(40 answers)
Closed 7 years ago.
I want to give input as
%>Python_script_name listed_files
How to check if exist as file or it is some input argument.
<listed file can also be in path /govr/rest/listed_files >
Please share the small script, by which I can check if command line argument is valid file or it is a simple variable to be processed.
Just to summarize, I need to process my script so as to take or a single argument at command line.
Please help.
Thanks in advance.
Short and simple Answer.
import os.path
os.path.isfile(fname)

Categories