Anthony Appleyard opened this issue on Jul 17, 2021 ยท 9 posts
Anthony Appleyard posted Sat, 17 July 2021 at 10:43 AM
In a Poser Python script, please, how can I do these?
(1) Open a file named "zxcvbnm.txt"
(2) Write the text "The quick brown fox" to that file.
(3) Close that file.
HartyBart posted Sat, 17 July 2021 at 11:48 AM
(1). This line in a PoserPython script will do the first part of what you want, at least it will on Windows. Here we open my_text_file.txt with My Text Editor:
subprocess.call([r'C:|Program Files|My_Text_Editor.exe',r'C:|my_text_file.txt'])
(The forum refuses to display backslashes, so pipes have been used above - but you get the idea and can repair it). Note that the r's are important.
(2). Not sure how you would have the script actually type something in my_text_file.txt, other than by launching a desktop macro such as JitBit MacroRecorder + pre-recorded macro by the same method. Possible? But you can have the script rename the file as something-something-something.txt, which may be just as useful. To do that you would use the os.rename command from within the script. (Having first called up the OS module in the script header).
(3). Not sure how you'd then kill the subprocess.call software that you had Poser launch. In some circumstances the called software will just exit when the task is done and the script will carry on. If you are passing the called software its own type of script, then you may be able to conclude that script with an explicit kill-switch that will terminate the .exe. An example of a Windows-only kill-switch for a Photoshop javascript would be:
executeAction(app.charIDToTypeID('quit'), undefined, DialogModes.NO);
Learn the Secrets of Poser 11 and Line-art Filters.
HartyBart posted Sat, 17 July 2021 at 1:13 PM
Wow, who knew - (2) can be done from PoserPython, and does not even need (1) and (3). It should then by possible to have the script write many of the scene settings to a text file, along with the render, as a memory jogger for a later time.
At its simplest, tested and working on Poser 11:
import poser
scene = poser.Scene()
textfile = open(r'C:|Users|YOURNAME|Desktop|example.txt', 'w')
a = textfile.write('The quick brown fox')
textfile.close()
The forum software does not allow backslashes, and these have been substituted with | pipes. Replace these before use.
Works, but as it is it overwrites whatever text is in the file already. But it can get a lot more complex than this - search Google for "Python write String to a file" to find guides with examples.
Learn the Secrets of Poser 11 and Line-art Filters.
Anthony Appleyard posted Sat, 17 July 2021 at 4:45 PM
HartyBart posted at 4:43PM Sat, 17 July 2021 - #4423399
Works, but as it is it overwrites whatever text is in the file already. But it can get a lot more complex than this - search Google for "Python write String to a file" to find guides with examples.
Thanks. In that case, we need a form with
textfile = open(r'C:|Users|YOURNAME|Desktop|example.txt', 'w')
replaced by a form that opens the file in append mode.
structure posted Sun, 18 July 2021 at 2:49 AM Forum Coordinator
you would need to open the file with the "a" clause to append the new text.
import os
import os.path
filepath = ('/path/to/file')
Are we creating a new file?
if not os.path.exists( os.dirname( filepath )):
os.makedirs( os.dirname( filepath ) )
if not os.path.exists( filepath ) :
with open( filepath, "w" ) as outfile:
outfile.write( "text to be written" )
Want to use an existing file?
if os.path.exists( filepath ):
with open( filepath, "a" ) as outfile:
outfile.write( "text to be appended" )
Using "with open" negates the need to remember to close the file when you are finished with it.
Python 3
from pathlib import Path
filepath = Path('/path/to/file')
if filepath.exists():
filepath.open("a")
filepath.write_text('text to be appended')
Locked Out
FVerbaas posted Tue, 03 August 2021 at 8:55 AM Forum Coordinator
Last snippet, 'python3' would need a line 'filepath.close()' when all writing actions are done, right?
structure posted Fri, 13 August 2021 at 4:56 AM Forum Coordinator
AFAIK it is not necessay in P3 - I will run tests as soon as I am able.
Locked Out
FVerbaas posted Fri, 13 August 2021 at 11:39 AM Forum Coordinator
Maybe the file will be closed at some point automatically, most likely when 'filepath' is redefined or scope does not exist anymore. I think that from a point of process control it is better to close consciously once you do not need to write anymore. Less potential collision, incomplete files because data is still in a buffer, and so on.
adp001 posted Sat, 14 August 2021 at 8:08 AM
structure posted at 8:02AM Sat, 14 August 2021 - #4425190
AFAIK it is not necessay in P3 - I will run tests as soon as I am able.
This is only true for files open for reading. And even then it is simply good practice to wrap file access in:
with open(filename, mode) as filehandle:
for line in filehandle:
print(line)
...
print("teststring", file=filehandle)
Read the docs if you need to know why. Your own test may only cover a fraction of the wisdom found in the Python documentation :)