0

Ok, I have a directory with many files and subdirectories; among these ones there are 20 directories called mbr001, mbr002, ... until mbr020 that are the ones I am interested in.

I want to write a program that iteratively goes into mbr001, do somethings, then to mbr002, do the same things, and so on.

A solution with Python's os.chdir('./mbr001') until 20 seems pretty inefficient.

Besides, when I am in such directories, let's say for example mbr012, I want to create a variable that has in its name the number of the mbr where I am, in this case 12. Something like variable = 'mbr012_file.dat'. (This variable is used in what I am doing inside each directory, not relevant here).

What I would need would be something like this (note this is pseudo-code):

for i in mbr[i]:
    variable = "mbr[i]_file.dat"
    ...

How can I do the loop and the variable naming? Thanks in advance.

David
  • 1,155
  • 1
  • 13
  • 35
  • Are you sure that `i` is both the loop variable *and* used to get the items to loop over? –  Nov 10 '16 at 13:02

4 Answers4

1

Use format:

for i in list_of_is:
    filename = "mbr{0:02d}_file.dat".format(i)
1

What about something like this ?

for i in range(1, 21):
    dn =  "./mbr{:03}".format(i)
    var = "mbr{:03}_file.dat".format(i)
    os.chdir(dn)
    # Do your stuff here
    #
    #
    os.chdir("..")
C.LECLERC
  • 510
  • 3
  • 12
  • 1
    `listdir` might not be want he wants to do and the `range` should be defined as `range(1, 21)` or use `i+1` instead of `i`. i would simpy suggest using absolute paths instead of relative ones to avoid having to `chdir` everytime – Ma0 Nov 10 '16 at 13:08
  • 1
    @Ev.Kounis I agree about your absolute path advise. But since there is no information about operations to do in those folders, i will keep it like thise for now. – C.LECLERC Nov 10 '16 at 13:17
  • What I am doing is to concatenate all the files in each `mbr...` directory; indeed using the solution from this post http://stackoverflow.com/questions/17749484/python-script-to-concatenate-all-the-files-in-the-directory-into-one-file The variable `outfilename` is what it would be here `var`. – David Nov 10 '16 at 13:25
  • I am checking @C.LECLERC 's solution. The size of my directories is pretty big... – David Nov 10 '16 at 13:28
  • Thank you. The solution is working but there seems to be a problem with the {:03} part. The file is named with a {:03}, not with the 001, or 010 or 020... as expected for each directory. – David Nov 10 '16 at 13:46
  • Ah, forget about this last comment. I forgot the `format(i)` in the `var`. Sorry. – David Nov 10 '16 at 13:47
1

You just need to concatenate '_files.dat' into the directory name

import re
for i in mbr_list:
    variable = i + '_files.dat'
    # use regex if you only interest on the numeric part
    # variable = re.sub('mbr(\d+)', r'mbr\1_file.dat', i)
    # do your thing here
Skycc
  • 3,496
  • 1
  • 12
  • 18
1

In python 3.4 and above you can use pathlib and in python 3.6 and above you can use "f string" to format text

from pathlib import Path

for path in [d for d in Path.cwd().iterdir() if d.match("mbr*")]:
    variable = f"{path.name}_file.dat"
    # do other stuff
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179