[Tutor] File access by os.system

Alan Gauld alan.gauld at btinternet.com
Tue May 15 09:44:11 CEST 2007


"lohith madireddy" <lohithreddym at gmail.com> 

>    I have files named 'x.pdb', 'y.pdb',..... in one directory. I am
> using python to read the files in that directory and do a system
> operation on each of the file using os.system. When I use this, it
> always gives an error saying 'could not read the file'.

There are several likely problems.
The first is that the files might not have security pernissions set 
for you to manipulate - can you do the system() command from 
the OS prompt oK?

Second, you are only passing the name of the file not the full 
path so the system command can't find the file. 

Third you could be passing something other than a 
file - eg another directory - to the command.

> import sys,os,tempfile,shutil
> Pdbs = os.listdir(os.getcwd())
> temp1=tempfile.TemporaryFile()
> for Pdb in Pdbs:

You probably need to use stat here to check that the item 
really is a file and that it has permissions.

Alternatively use a try except clause and if an exception 
is raised simply use continue to start the next loop iteration.
(possibly after printing an error message with the pdb name 
in it so you can check why it failed later)

>    print(type(Pdb))
>    os.system("dsspcmbi -v Pdb temp1")

You can get more about doing stat checks in the OS topic 
of my tutorial.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list