Python code not writing to file unless run in interpreter - python

I have written a few lines of code in Python to see if I can make it read a text file, make a list out of it where the lines are lists themselves, and then turn everything back into a string and write it as output on a different file. This may sound silly, but the idea is to shuffle the items once they are listed, and I need to make sure I can do the reading and writing correctly first. This is the code:
import csv,StringIO
datalist = open('tmp/lista.txt', 'r')
leyendo = datalist.read()
separando = csv.reader(StringIO.StringIO(leyendo), delimiter = '\t')
macrolist = list(separando)
almosthere = ('\t'.join(i) for i in macrolist)
justonemore = list(almosthere)
arewedoneyet = '\n'.join(justonemore)
with open('tmp/randolista.txt', 'w') as newdoc:
newdoc.write(arewedoneyet)
newdoc.close()
datalist.close()
This seems to work just fine when I run it line by line on the interpreter, but when I save it as a separate Python script and run it (myscript.py) nothing happens. The output file is not even created. After having a look at similar issues raised here, I have introduced the 'with' parameter (before I opened the output file through output = open()), I have tried flushing as well as closing the file... Nothing seems to work. The standalone script does not seem to do much, but the code can't be too wrong if it works on the interpreter, right?
Thanks in advance!
P.S.: I'm new to Python and fairly new to programming, so I apologise if this is due to a shallow understanding of a basic issue.

Where are the input file and where do you want to save the output file. For this kind of scripts i think that it's better use absolute paths
Use:
open('/tmp/lista.txt', 'r')
instead of:
open('tmp/lista.txt', 'r')
I think that the error can be related to this

It may have something to do with where you start your interpreter.
Try use a absolute path /tmp/randolista.txt instead of relative path tmp/randolista.txt to isolate the problem.

Related

Script failing to create a text file on code run

I have come across a very strange issue, I am using miniconda to test out the GPT-NEO ai text generator, I would call it like this in the command prompt:
C:\tools\miniconda3\python C:\Users\Graham\Desktop\Files\programming\Languages\Python\gpt_neo_app\ai.py
Python Code:
from os import system
from transformers import pipeline
import json
try:
# generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B', device=-1)
generator = pipeline('text-generation', model='EleutherAI/gpt-neo-1.3B', device=-1)
outjson = generator("unlock your hip flexors", do_sample=True, max_length=500, temperature=1.9)
outtext = json.loads(json.dumps(outjson[0]))["generated_text"]
# with open(r"C:\Users\Graham\Desktop\Files\programming\Languages\Python\gpttext.txt", "w") as f:
with open("gpttext.txt", "w") as f:
f.write(outtext)
print(outtext)
except Exception:
pass
The part that fails is writing to the .txt file, no matter what i do (even commenting out the text generation code and just putting in a random string to be written) the .txt file is never created or written to.
The text generator is working fine, i even tried the full path to the .txt file this still never worked, such a basic issue i cannot seem to see the problem, is there anything i have missed or have done wrong? it seems fairly straight forward enough but it just will not be created.
Any help would be appreciated.
Just posting incase anyone else has this issue, I needed to put the full path to the .txt file.

is there a way of making a file-ception with python?

i wanted to make a python file that makes a copy of itself, then executes it and closes itself, then the copy makes another copy of itself and so on...
i am not asking for people to write my code and this could be taken as just a fun challenge, but i want to learn more about this stuff and help is appreciated.
i have already played around with it but can't wrap my mind around it,
I've already tried making a py file and just pasting a copy of the file itself in it two different ways i could think of but it would just go on forever.
#i use this piece of code to easily execute the py file using os
os.startfile("file.py")
#and to make new py file i just use open()
file = open("file.py","w")
file.write("""hello world
you can use 3 quote marks to write over multiple lines""")
i expect that when you run the program it makes a copy of itself, runs it, and closes itself, and the newly ran program loops over.
what actually happenes is that either I'm writing code forever or,
when i embed the code it pastes in the copy of itself into what it copies to the copy file,
it rightfully says it doesn't know what that code is because it's being written.
it's all really confusing and this is difficult to explain and I'm sorry
it's midnight atm and I'm tired.
I don't have enough rep to reply to #Prune:
os.startfile(file) only works on Windows, and is replaced by subprocess.call
shutil.copy2(src, dst) works on both Windows and Linux.
Try this solution as well:
import shutil
import subprocess
old_file = __file__
new_file = generate_unique_file_name()
shutil.copy2(old_file, new_file) # works for both Windows and Linux
subprocess.call('python {}'.format(new_file), shell=True)
You're close; you have things in the wrong order. Create the new file, then execute it.
import os
old_file = __file__
new_file = generate_unique_file_name()
os.system('cp ' + old_file + ' ' + new_file) #UNIX syntax; for Windows, use "copy"
os.startfile(new_file)
You'll have to choose & code your preferred method for creating a unique file name. You might want to use a time-stamp as part of the name.
You might also want to delete this file before you exit; otherwise, you'll eventually fill your disk with these files.

Execute python script in Qlik Sense load script

I am trying to run python script inside my load script in Qlik Sense app.
I know that I need to put OverrideScriptSecurity=1 in Settings.ini
I put
Execute py lib://python/getSolution.py 100 'bla'; // 100 and 'bla' are parameters
and I get no error in qlik sense, but script is not executed (I think) because inside the script I have
f = open("file.xml", "wb")
f.write(xml)
f.close
and file is not saved.
If I run script from terminal, then script is properly executed.
What could go wrong?
By the way, my full path to python interpreter is
C:\Users\Marko Z\AppData\Local\Programs\Python\Python37-32\python.exe
EDIT :
Even if I add this
Set vPythonPath = "C:\Users\Marko Z\AppData\Local\Programs\Python\Python37-32\python.exe";
Set vPythonFile = "C:\Users\Marko Z\Documents\Qlik\Sense\....\getSolution.py";
Execute $(vPythonPath) $(vPythonFile);
I get the same behaviour. No error, but not working,...
I even see that if I change path (incorrect path) it give me an error, but incorrect file it doesn't give me an error.... (but I am sure it is the right file path...)
My python code is
xml = "Marko"
xml = xml.encode('utf-8')
f = open("C:\\Users\\Marko Z\\Test.xml", "wb")
f.write(xml)
f.close
I figure out what was wrong.
For all others that would have similar problems:
Problem is in space in path.
If I move my script in c:\Windows\getSolution.py it work. I also need to change the python path to c:\Windows\py.exe
so end script looks like:
Execute c:\Windows\py.exe c:\Windows\getSolution.py 100 'bla';
But I still need to figure how to work with space in path...
Strange. With exactly the same python file and QS script the result file is generated correctly.
The content of my settings.ini.
[Settings 7]
StandardReload=0
OverrideScriptSecurity=1
According to Qlik's documentation there should be an empty line at the end (point 4 from the lists)
I was able to get the below to work in qlik sense:
set vPyExe = C:\Program Files\Python37\python.exe;
set vPyScript = D:\...\PythonScript.py;
Execute
"$(vPyExe)" "$(vSource)"
;

Interpolate variables into external programs call (subprocess.call/Popen) in Python

I'm trying to run an external program from a Python script.
After searching and reading multiple post here I came to what seemed to be the solution.
First, I used subprocess.call function.
If I build the command this way:
hmmer1=subprocess.call("D:\Python_Scripts\HMMer3\hmmsearch.exe --tblout hmmTestTab.out SDHA.hmm Test.fasta")
The external program D:\Python_Scripts\HMMer3\hmmsearch.exe is run taking hmmTestTab.out as file name for the output and SDHA.hmm and Test.fasta as input files.
Nevertheless, if I try to replace the file names with the variables outfile, hmmprofile and fastafile (I intend to receive those variables as arguments for the Python script and use them to build the external program call),
hmmer2=subprocess.call("D:\Python_Scripts\HMMer3\hmmsearch.exe --tblout outfile hmmprofile fastafile")
Python prints an error about being unable to open the input files.
I also used "Popen" function with analogous results:
This call works
hmmer3=Popen(['D:\Python_Scripts\HMMer3\hmmsearch.exe', '--tblout','hmmTestTab.out', 'SDHA.hmm','Test.fasta'])
and this one doesn't
hmmer4=Popen(['D:\Python_Scripts\HMMer3\hmmsearch.exe', '--tblout','outfile', 'hmmprofile','fastafile'])
As result of this, I presume I need to understand which is process to follow to interpolate the variables into the call, because it seems that the problem is there.
Would any of you help me with this issue?
Thanks in advance
You have:
hmmer4=Popen(['D:\Python_Scripts\HMMer3\hmmsearch.exe', '--tblout','outfile', 'hmmprofile','fastafile'])
But that's not passing the variable outfile. It's passing a string, 'outfile'.
You want:
hmmer4=Popen(['D:\Python_Scripts\HMMer3\hmmsearch.exe', '--tblout', outfile, hmmprofile, fastafile])
And the other answer is correct, though it addresses a different problem; you should double the backslashes, or use r'' raw strings.
Try to change this:
hmmer1=subprocess.call("D:\Python_Scripts\HMMer3\hmmsearch.exe"
to
hmmer1=subprocess.call('D:\\Python_Scripts\\HMMer3\\hmmsearch.exe'
Edit
argv = ' --tblout outfile hmmprofile fastafile' # your arguments
program = [r'"D:\\Python_Scripts\\HMMer3\\hmmsearch.exe"', argv]
subprocess.call(program)

Python OSError: Too many open files

I'm using Python 2.7 on Windows XP.
My script relies on tempfile.mkstemp and tempfile.mkdtemp to create a lot of files and directories with the following pattern:
_,_tmp = mkstemp(prefix=section,dir=indir,text=True)
<do something with file>
os.close(_)
Running the script always incurs the following error (although the exact line number changes, etc.). The actual file that the script is attempting to open varies.
OSError: [Errno 24] Too many open files: 'path\\to\\most\\recent\\attempt\\to\\open\\file'
Any thoughts on how I might debug this? Also, let me know if you would like additional information. Thanks!
EDIT:
Here's an example of use:
out = os.fdopen(_,'w')
out.write("Something")
out.close()
with open(_) as p:
p.read()
You probably don't have the same value stored in _ at the time you call os.close(_) as at the time you created the temp file. Try assigning to a named variable instead of _.
If would help you and us if you could provide a very small code snippet that demonstrates the error.
why not use tempfile.NamedTemporaryFile with delete=False? This allows you to work with python file objects which is one bonus. Also, it can be used as a context manager (which should take care of all the details making sure the file is properly closed):
with tempfile.NamedTemporaryFile('w',prefix=section,dir=indir,delete=False) as f:
pass #Do something with the file here.

Categories

Resources