![]() echoes the clear command if in Git Bash, or cls if in cmd. For example, I'm trying to write a function to clear the terminal (regardless of which terminal it is), e.g. Movie A (2014)/fileb.srt Movie A (2014)/Movie A (2014).srt I'd like to give my Python scripts the ability to detect whether it was executed in a Git Bash terminal, or the Windows cmd command line interface. In this particular case, we can use it to filter out all files, and perform a renaming operation on them.įirst of all, let me give you the solution: find -type f -exec bash -c 'fp=$(dirname "$1") fn=$(basename "$fp") px="$" echo "$f" "$fp"/"$fp"."$ext" done ![]() Solution via find command and -exec flagįind command is useful in many ways, particular when performing operations on multiple levels of directory tree. Removing subtitles: mkvmerge -o output.mkv input.mkv -S remove all subtitle tracks, note the uppercase S mkvmerge -o output.mkv input.mkv -s 3,4 include only subtitle tracks 3 and 4 mkvmerge -o output.mkv input.mkv -s 3 include all subtitle tracks except 3 mkvmerge -i input.mkv show track numbers. Move /home/xieerqi/testdir/Movie B (2016)/filea.mkv to. Move /home/xieerqi/testdir/Movie B (2016)/fileb.srt to. Move /home/xieerqi/testdir/Movie A (2014)/filea.mkv to. Move /home/xieerqi/testdir/Movie A (2014)/fileb.srt to. So when we run the script, we shall see the following output: $. Our script lives in the same directory: $ tree I've created a directory with two others, Movie A (2016) and Movie B (2016). Here's an example of how this works in practice. save it as add_location_name.py in the top-most directory Setting up the scriptĪll the standard rules for scripting apply: NOTE: very very important is that to actually rename the files you need to remove # before # os.rename(fullpath,newpath). ![]() Print('Move ' + fullpath + ' to ' + newpath ) Newpath = os.path.join(dir, dir + '.' + extension) Script contents #!/usr/bin/env python3įor dir,subdirs,files in os.walk(treeroot):įullpath = os.path.realpath( os.path.join(dir,f) ) In the script presented below we're doing exactly that - we're finding all files and operating on each one of them, by determining full path to each file, file's extension, and renaming it via os.rename() function. Python is a quite powerful language for system administration, and traversing directory tree can be done via os.walk() function. Please read instructions carefully for best results. There are multiple ways to approach this question.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |