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

  • SEARCH
  • Home
  • 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 7557821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:13:56+00:00 2026-05-30T12:13:56+00:00

my python script is the following code: 1 import subprocess 2 3 # initial

  • 0

my python script is the following code:

 1 import subprocess
 2 
 3 # initial 'output' to make
 4 r0 = 21
 5 # number of runs to make
 6 rn = 10
 7 
 8 rf = r0+rn
 9 
10 for i in range(r0, rf):
11   #output directory
12   opt_dir = 'output'+str(i)
13   #put it in this output directory
14   popt_dir = './output'+str(i)
15 
16   subprocess.call(['mkdir', opt_dir])
17   subprocess.call(['./exp_fit', 'efit.inp'])
18   subprocess.call(['mv', 'at*', popt_dir])

the intention is this:

i have a program called “exp_fit” which takes an input file “efit.inp”. one call to ./exp_fit efit.inp will create output files called ‘at0_l0_l0’, ‘at0_l1_l-1’, … etc (total 475 files starting with ‘at’).

now, i have been generating data files by running ‘exp_fit’, then creating output directories and moving them into the output directories with the following bash commands:
(for example, with the 20th run of my code)

mkdir output20

mv at* ./output20

so i would think that my script should do the same thing. however, it only does the following:

(1) it correctly generates all output files (475 files starting with ‘at’)
(2) it correctly creates the desired directories (output21 – output30)
(3) it DOES NOT, however, correctly move all the output files starting with ‘at’ into the desired directories. why is this? shouldn’t the call to line 18 correctly execute the command to move all my files starting with ‘at’ into the desired directory?

should i be writing this script with bash instead of python? what is wrong with this?

  • 1 1 Answer
  • 0 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-30T12:13:57+00:00Added an answer on May 30, 2026 at 12:13 pm

    The problem is that this subprocess command

    subprocess.call(['mv', 'at*', './output20'])
    

    is not the same as typing this at a prompt

    $ mv at* ./output20
    

    In the latter case, the bash glob expansion converts the single at* argument to a list of arguments of matching filenames for the mv command. So the kernel sees the second as

    ['mv', 'at0_l0_l0', 'at0_l1_l-1', './output20']
    

    kev’s answer tells Python to pass the command through the shell, so the escaping will occur.

    But the better solution is to use the glob module and os.rename libraries and not call the subprocess. Creating subprocesses is expensive, and using shell=True could lead to security holes, so it’s best to avoid that habit.

    (Actually, I suggest making the output directory, switching into it, and then running the exp_fit program from within that directory. Then you won’t have to move the output. Try that first.)

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

Sidebar

Related Questions

I've come across the following code in a Python script from pprint import pprint
I am trying to execute the following code in Python import os,subprocess from glob
Consider the following Python code: import os print os.getcwd() I use os.getcwd() to get
I have the following code, copied from the Python manual: import logging LOG_FILENAME =
I have written following script in python which works fine: from sys import argv
I am trying to run a python script which has the following statements: import
I have the following python script and it works beautifully. import urllib2 url =
i have something similar to the following... used a python script to JSON to
I have the following simple python test script that uses Suds to call a
I'm having terrible trouble trying to understand python scoping rules. With the following script:

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.