简体   繁体   中英

Python rename files in subdirectory with subdirectory name

I have a directory which contains many subdirectories, and each subdirectory contains many files. There are specific files that I want to copy and they all share the same name. The way to distinguish is the subdirectory name. Example:

parent/$date/subdirectory1/file.foo
parent/$date/subdirectory2/file.foo

I want to copy all the file.foo to a new location and rename them date_subdirectory1_file.foo

I tried using os.rename but that only worked for one file at a time and I can't figure out a recursive with the date and subdirectories as params

solved with a bash script

shopt -u globstar
for csv in **/file.foo; do
  mv "$csv" "${csv//\//_}"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM