Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1012239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:54:57+00:00 2026-05-16T09:54:57+00:00

I was wondering whether or not it is considered a good style to call

  • 0

I was wondering whether or not it is considered a good style to call bash commands within a Python script using os.system(). I was also wondering whether or not it is safe to do so as well.

I know how to implement some of the functionality I need in Bash and in Python, but it is much simpler and more intuitive to implement it in Bash. However, I feel like it is very hackish to write os.system(“bash code”).

Specifically, I want to move all files that end with a certain extension to a directory.

In bash: *mv .ext /path/to/destination
In Python (Pseudocode):
for file in directory:
if file.endswith(“ext”):
move file to destination

In this case, what should I do?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:54:58+00:00Added an answer on May 16, 2026 at 9:54 am

    First of all, your example uses mv, which is a program in coreutils, not bash.

    Using os.system() calls to external programs is considered poor style because:

    • You are creating platform-specific dependencies
    • You are creating version-specific dependencies (Yes, even coreutils change sometimes!)
    • You need to check for the existence of external commands (and that they are in $PATH, and executable by the user etc.)
    • You have to wrap the commands with error checking using their return code. It is much nicer to use in-language error-codes or exceptions. (os.system() does not let you parse stdout/stderr)
    • You have to deal with quoting variables with spaces yourself (or escaping them)
    • Python has already done the work for you by supplying the libraries!

    Look up glob, for shell-like pattern matching (globbing), and shutil, as others have already mentioned. Otherwise, everything you need is already in the standard libraries.

    import glob
    import shutil
    
    for extfile in glob.glob('*.ext'):
        shutil.move(extfile,dest)  
    

    In addition, os.system() should not be used – take a look at the subprocess module instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering whether or not I've to call the $stmt->free_result() for after I've
I'm wondering whether or not there are any restrictions during HBase minor/major compactions .
Hi just wondering whether or not to add a pull to refresh in my
I was wondering whether or not I can extend the Enum type in C#
This might be a stupid question, but I was wondering whether or not you
I was wondering, whether is AndroidHttpClient thread safe, as this is not being mentioned
I'm wondering if writing functions like this is considered good or bad form. def
I am wondering whether or not it is possible to output the class name
I'm wondering whether it's considered okay to do something like this. if ( p_Pointer
I am wondering whether or not it is possible to elegantly map the results

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.