MarrT
MarrT

Reputation: 21

Which is better? Using inbuilt python functions or os.system commands?

Which is better to use in a python automation script for following simple operations To create a zip file and copy it or rename it to a new location. Using python inbuilt functions or terminal commands through os.system modules is better?

Upvotes: 2

Views: 233

Answers (5)

bluszcz
bluszcz

Reputation: 4128

Using python internals command is nice, especially in terms of portability.

But at some point, you can be confused by lack of "os.kill" in Python older than 2.7 (Windows), you can be surprised by way how os.Popen is working, than you will discover win32pipe etc etc.

Personally I would suggest always a small research (do you need daemons etc) and then decide. If you don't need windows platform - using python's internals could be more efficient.

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336418

I'd use the Python library if possible. The biggest drawbacks of the built-in functions appear to me that they often don't support all the features of the underlying OS, for example Alternate Data Streams or Access Control Lists etc. under NTFS may get lost in a move operation.

Upvotes: 4

andrewmu
andrewmu

Reputation: 14534

In general I'd say use the python libraries where possible - that way it'll be more portable, e.g. you won't need to worry about different commands or command options on various systems, and also if you need to change anything it's easier to do just python code.

Upvotes: 2

user225312
user225312

Reputation: 131737

The inbuilt Python modules/ stdlib wherever you can, subprocess (os.system) where you must.

Reasons: Portability, maintenance, code readability just to name a few.

Upvotes: 11

Eric Fortin
Eric Fortin

Reputation: 7603

I would say python ones since it'll make the script portable. But sometimes, performance and availability are of concerns.

Upvotes: 2

Related Questions