OT: unix newbie questions
Fredrik Lundh
fredrik at pythonware.com
Tue Jun 6 02:36:59 EDT 2006
Carlos Lopez wrote:
> Please help i am losing my mind ... UNIX Newbee
>
> *23. How do you add a line to the end of an existing file "myfile" with
> date stamp. (1) *
>>> f = open("myfile", "a+")
>>> f.write(datestamp)
>>> f.close()
> *24. Display recent 10 java files, (with *.java extension) , in
> descending order by time, latest to oldest time. (1) *
>>> files = sorted(glob.glob("*.py"), key=os.path.getmtime)[-10:]
>>> files.reverse()
> *25. How do you set only read permissions to user, group and others in
> octal mode for a file "myfile.txt" ? (2)
>>> os.chmod("myfile.txt", 0444) # note the leading zero
> *26. You observed that some of your group members are fiddling with your
> file "myfile" and you wanted to remove the read permission to your
> group. How do you do? (1)
>>> os.chmod("myfile.txt", 0404)
> *28. Here is another long listing of a file. (1)*
> -rw-r----- 1 Y435678 odms 20 Sep 02 17:03 file.txt. ***
>
> *What are the owner permissions? *
>>> s = os.stat("urllib.py").st_mode
>>> if s & stat.S_IREAD: print "READ"
>>> if s & stat.S_IWRITE: print "WRITE"
>>> if s & stat.S_IEXEC: print "EXEC"
> *29. The file “users_data” has the following contents : (1)
> Tom Smith 7.00 15 105.00
> Rob Sheryl 8.00 20 160.00
> Ken Bradman 7.00 13 91.00
> Peter Smith 6.00 15 90.00
> Dennis Smith 8.00 13 104.00
> Tom Dave 9.00 12 108.00 *
>
> *How do you sort the above file and redirect the output to another file
> called “sortedusers” *
>>> out = open("sortedusers", "w")
>>> out.writelines(sorted(open("users_data")))
> *20. What is the command to list files in a directory : (2)
>>> os.listdir(directory)
or, if you want full path names and glob-style filtering:
>>> glob.glob(os.path.join(directory, "*"))
</F>
More information about the Python-list
mailing list