0

I have Python set up to create and open a txt file [see Open document with default application in Python ], which I then manually make some changes to and close. Immidiately after this is complete I want Python to open up next txt file. I currently have this set up so that python waits for a key command that I type after I have closed the file, and on that key, it opens the next one for me to edit.

Is there a way of getting Python to open the next document as soon as the prior one is closed (i.e to skip out having python wait for a key to be clicked). ... I will be repeating this task approximately 100,000 times, and thus every fraction of a second of clicking mounts up very quickly. I basically want to get rid of having to interface with python, and simply to have the next txt file automatically appear as soon as prior one is closed.

I couldn't work out how to do it, but was thinking along the lines of a wait until the prior file is closed (wasn't sure if there was a way for python to be able to tell if a file is open/closed).

For reference, I am using python2.7 and Windows.

Community
  • 1
  • 1
kyrenia
  • 5,431
  • 9
  • 63
  • 93
  • This is highly application-specific. In most cases you can wait until the process dies. – simonzack Nov 24 '14 at 04:03
  • 1
    You could check the modified time of files: http://stackoverflow.com/a/237084/3651800 – Matt Coubrough Nov 24 '14 at 04:03
  • 1
    You're manually editing 100,000 files? Yikes. – Ryne Everett Nov 24 '14 at 04:08
  • @RyneEverett - I thankfully have someone else who will be doing this rather dull job!! – kyrenia Nov 24 '14 at 04:12
  • @MattCoubrough - thanks - I will try this (probably a some sort of loop until modified time is different to created time) and edit post when i have got it to work. – kyrenia Nov 24 '14 at 04:14
  • @kyrenia Depending on how long the editing of the document can take, you might want to run a periodic task in a thread to check the modified time of the last opened file - a continuous blocking loop checking modified times wouldn't be kind to the system. Depending on the nature of the text file editing, could you perform the editing via code, or construct a simple GUI that makes it easy for the user to perform the editing, save, and continue with the next file? – Matt Coubrough Nov 24 '14 at 04:18

3 Answers3

5

Use the subprocess module's Popen Constructor to open the file. It will return an object with a wait() method which will block until the file is closed.

Ryne Everett
  • 6,427
  • 3
  • 37
  • 49
  • Just a note to share: make sure that the process you want to wait for is actually the process, which you have in your `subprocess.Popen([parameters]).pid`. I tried to make script wait for Microsoft Word to exit, but then I found out, that actual `pid` was id of some background process, that launched Word (console maybe?) and terminated immidiately, and script would execute further, while Word is still running in the background. Besides Word has single process and multiple threads for opened documents, so if you want to wait for document closed - that's a problem... – Constantine Ketskalo Jun 08 '19 at 22:39
0

In case of interest, the following code using the modified time method worked:

os.startfile(text_file_name)        
  modified = time.ctime(os.path.getmtime(text_file_name))
created = time.ctime(os.path.getctime(text_file_name))

  while modified == created:
  sleep(0.5)    
modified = time.ctime(os.path.getmtime(text_file_name)) 
  print modified

  print "moving on to next item"
  sleep(0.5)
sys.stdout.flush()

Athough I think I will use the Popen constructor in the future since that seems a much more elegant way of doing (and also allows for situations where the file is closed without an edit been needed).

BenC
  • 8,729
  • 3
  • 49
  • 68
kyrenia
  • 5,431
  • 9
  • 63
  • 93
-1

How about something like:

for fname in list_of_files:
    with open(fname, mode) as f:
        # do stuff
David Marx
  • 8,172
  • 3
  • 45
  • 66