0

In a directory "A" i have several directories names "abc_123","abc_124" and so on. also in directory "A" i have directories named "anc_123","anc_124" and so on. and also several other directories in it.

My question is how would i move only directories starting with "abc_***" to a different location. I'm new to python and don't know how to proceed on this one. Any help is appreciated.

A B
  • 1
  • 1
  • 1
    In bash: `mv abc_* new_location/` – apilat Aug 23 '21 at 21:42
  • 1
    I'm trying to write a python program, does the above answer apply as well – A B Aug 23 '21 at 21:44
  • The simplest solution would be to avoid Python and use command-line tools which are designed for moving files. If you insist on doing this in Python, you will need to use the functions in the [`os`](https://docs.python.org/3/library/os.html) module, likely at least `listdir` and `rename`. – apilat Aug 23 '21 at 21:57
  • If you'd like to use the shell route, python is good at executing shell commands; see https://stackoverflow.com/q/89228/1275942 for a number of ways to do it. That said, it's fine to do it within python as well--python's support for file operations is about as good, if you're used to it. That also has the benefit of being more portable--if you're running your code on windows, bash might be iffy, but python's libraries hopefully still work. – Kaia Aug 23 '21 at 22:03

1 Answers1

0

shutil's move(src, dst) method will move a directory.

os.listdir(dir) can list all files and directories within a directory. You could then simply do a string comparison to test if the directories start with abc.

However, glob might be the best choice. glob.glob("directory/abc_*") will return a list of all files/directories of directory starting with abc.

You'll also want to check if the files are files or directories, for which os.path.isdir is useful.

import glob
import os
import os.path
import shutil

files = glob.glob("abc_*") # will return a list, like ["abc_123", "abc_345"]
for file in files:
    if os.path.isdir(file):
        shutil.move(file, "dest/")
hd1
  • 33,938
  • 5
  • 80
  • 91
Kaia
  • 862
  • 5
  • 21
  • when I try the above code its giving me empty list in files=glob.glob("abc.*") step – A B Aug 23 '21 at 22:21
  • @apilat I'm trying to implement this functionality in python program – A B Aug 23 '21 at 22:22
  • `_`, not `.`, assuming your file is `abc_123`. Read the examples on the glob docs page for more information on how to customize the glob – Kaia Aug 23 '21 at 22:24
  • https://pymotw.com/2/glob/ looks like it also has examples – Kaia Aug 23 '21 at 22:26
  • that was a typo, but when I'm trying it and print(files) i get empty list – A B Aug 23 '21 at 22:33
  • This is probably a issue with working directories. `os.getcwd()` will give you the directory that python is executing in. You need to provide a relative path from that directory to the directory where the `abc` folders are. You can also provide an absolute path – Kaia Aug 24 '21 at 17:32