[Pythonmac-SIG] MacPython OSX beginner concepts ~/Desktop

Schollnick, Benjamin Benjamin.Schollnick at xerox.com
Tue Jan 11 14:34:12 CET 2005


 > In apple's terminal app I can type:
 > 
 > touch ~/Desktop/foobar
 > 
 > and create an empty file.
 
 Keep in mind Touch is not just used to create a empty file...
 
 To create a empty file...
 
 def	file_to_create ( filepathname ):
 	empty = open ( filepathname, "w" )
 	empty.close ()
 
 
 But touch can be used to update a existing file's time/date stamp.
 
 Here's a limited tested Touch clone:
 
 def touch ( filepathname, atime = None, mtime = None ):
     """An attempt to recreate in python Unix's Touch command...
 
     Set the access and modified times of the file specified by path.
     If atime and mtime are set to None (Default), then the 
 file's access
     and modified times are set to the current time. Otherwise, atime
     (Last Accessed) time, and mtime (Last Modified) will be used to
     set the Last Accessed and Last Modified timedate stamp on 
 the file.
 
     inputs -
             filepathname    - The full filename and pathname of the
file to
                                 be modified or created.  The file will
be
                                 created if it does not exist.
             atime           - The last Accessed time to set the file
to.  
             mtime           - The last modified time to set the file
to.
 
     Outputs -
             External ; filepathname is either created, or the
             time/date stamp is updated to reflect the current time,
             or the times passed in by atime and mtime.
             
     """
     import os, os.path, time
     
     if os.path.exists (filepathname):
         #
         #   File Exists
         #
         if atime == None and mtime == None:
             #
             #   Update to current time
             #
             os.utime( filepathname, None)
         else:
             #
             #   Update to user definied times
             #
             if atime == None:
                 #
                 #   Only one of the variables is None, set it to NOW
                 #
                 atime = time.time()
             if mtime == None:
                 #
                 #   Only one of the variables is None, set it to NOW
                 #
                 mtime = time.time()
             os.utime (filepathname, (atime, mtime) )
     else:
         empty = open ( filepathname, "w" )
         empty.close ()
         if atime <> None and mtime <> None:
             #
             #   The user wants the newly created file to be 
             #   time/date stamped differently then "Now".
             #   Run the newly created file through touch again
             #   to define the atime and mtime.
             #
             touch ( filepathname, atime, mtime)
     
 
     


More information about the Pythonmac-SIG mailing list