How can I create a text file from an executable?

11 views
Skip to first unread message

Jose Luis Da

unread,
Feb 23, 2015, 5:52:48 AM2/23/15
to pyins...@googlegroups.com
Hi all,

I need to create some text files from an executable file. I am using the following code to test it, and while the script creates the text file when I call it from the terminal, the executable does not creates the text file. 

import sys, os
if getattr(sys, 'frozen', False):                                                              # path is different
application_path = os.path.dirname(sys.executable)                  # if its an executable
elif __file__:                                                                                         # or a Python script,
application_path = os.path.dirname(__file__)                             # this looks for it
 
with open('test.txt','w') as f:
f.write('test worked')

I am using 'os.path.dirname()' to get the path of the executable file and see if it creates the text file in another directory, but this does not seem to be the problem.

In order to create the executable file, I use the following commands:

python utils/makespec.py --onefile txtfomexe.py
python utils/build.py txtfromexe/txtfromexe.spec 

Should I use any additional option to these commands? What should I do to create the file?

I appreciate your help, thank you,
jl 

Hartmut Goebel

unread,
Feb 23, 2015, 6:03:45 AM2/23/15
to pyins...@googlegroups.com
Am 23.02.2015 um 11:52 schrieb Jose Luis Da:
with open('test.txt','w') as f:
f.write('test worked')

Obviously this file is to be created in the current working directory, because you are not adding any path to it. Try using an absolut path like /tmp/text.txt or C:\text.txt


--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Blog: http://www.goebel-consult.de/blog/tr-069-auf-fritzbox-ausschalten-und-ergebnis-prufen
Kolumne: http://www.cissp-gefluester.de/2012-04-compliance-bringt-keine-sicherheit

Da, Jose Luis

unread,
Feb 23, 2015, 6:08:02 AM2/23/15
to pyins...@googlegroups.com
Hi Hartmut, 
Thank you for your answer.

I understand that the file is to be created in the current directory with this code, When running the script, the text file is created successfully in the working directory. But the when running the executable, the file is not created at all.

--
You received this message because you are subscribed to a topic in the Google Groups "PyInstaller" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyinstaller/FYR1roP6sb0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyinstaller...@googlegroups.com.
To post to this group, send email to pyins...@googlegroups.com.
Visit this group at http://groups.google.com/group/pyinstaller.
For more options, visit https://groups.google.com/d/optout.

Da, Jose Luis

unread,
Feb 23, 2015, 6:09:55 AM2/23/15
to pyins...@googlegroups.com
I used  'os.path.dirname()' to see if the executable was running from another directory than the one the executable file actually is, and therefore creating the text file there.

Hartmut Goebel

unread,
Feb 23, 2015, 6:11:51 AM2/23/15
to pyins...@googlegroups.com
Am 23.02.2015 um 12:07 schrieb Da, Jose Luis:
But the when running the executable, the file is not created at all.

I doubt this. Have you tried using an absolute path? What error messages do you get?

--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Hartmut Goebel

unread,
Feb 23, 2015, 6:13:13 AM2/23/15
to pyins...@googlegroups.com
Am 23.02.2015 um 12:09 schrieb Da, Jose Luis:
used  'os.path.dirname()' to see if the executable was running from another directory than the one the executable file actually is, and therefore creating the text file there.

os.path.dirname does *not* give you the current working directory. os.getcwd() does.

--
Schönen Gruß
Hartmut Goebel
Dipl.-Informatiker (univ), CISSP, CSSLP
Information Security Management, Security Governance, Secure Software Development

Goebel Consult, Landshut
http://www.goebel-consult.de

Da, Jose Luis

unread,
Feb 23, 2015, 6:14:27 AM2/23/15
to pyins...@googlegroups.com
What do you mean using an absolute path? Instead of saving the file as 'test.txt', puting something like 'C:\user\desktop\text.txt',? I have not tried that, I will do it now.

I do not get any error messages, the executable runs fine.

--

Da, Jose Luis

unread,
Feb 23, 2015, 6:35:42 AM2/23/15
to pyins...@googlegroups.com
Thank you Hartmut, 
I finally found out what the problem was, I was not joining the path where the executable file is with the name of the output files. The files were saved in the working directory (os.getcwd()).
Reply all
Reply to author
Forward
0 new messages