-2

I have the python script below to iterate over all files ending with 'mkv', and print the same string without the 'mkv' at the end.

But, instead it prints the original filename including the 'mkv', why??

files=os.system('find /media/radamand/230_GB -name *mkv')
for file in str(files):
  converted_filename=file[0:-3]
  print(converted_filename)
Radamand
  • 175
  • 1
  • 12
  • while are you iterating over the string of files, wouldn't you want that to be a list? – Chris Oct 27 '22 at 21:07
  • Tried that, it complains aboiut some of the files with TypeError: 'int' object is not iterable – Radamand Oct 27 '22 at 21:09
  • Iterating over a string gives individual characters. What do you think `x[0:-3]` should print if `x` is a single character? – Pranav Hosangadi Oct 27 '22 at 21:10
  • didnt know that. okay so how should I make this work? – Radamand Oct 27 '22 at 21:10
  • Read the docs and inspect `files` to see what your `os.system` call returns. You want a _list_ of all files, where each item is an individual file. [There are other functions](https://stackoverflow.com/questions/2225564/get-a-filtered-list-of-files-in-a-directory) that will let you find all files matching a pattern. There is also [an existing function](https://stackoverflow.com/questions/8384737/extract-file-name-from-path-no-matter-what-the-os-path-format) to get the base name of a file, no need to reinvent the wheel – Pranav Hosangadi Oct 27 '22 at 21:13
  • 2
    `os.system` returns the exit code, an integer, not the text output. – Mark Ransom Oct 27 '22 at 21:13
  • then why does it still print the filenames? – Radamand Oct 27 '22 at 21:14
  • The output is coming from the `find` command itself, not from Python. – Mark Ransom Oct 29 '22 at 03:43

1 Answers1

1

Your os.system call executes your find command, sends its output to your interpreter standard output stream (which is why you're seeing your matching files including the "mkv" at the end, as this output is not the result of your print function in your later code), and then simply returns the exit code.

So your files variable actually gets an assignment of the integer 0.

Your for loop then casts files from an int into a string ('0') and thus your for loop now actually means: "loop through each character of the string files" (there is only one however), which, in this case, due to your slicing of [:-3] on a string of only one character, evaluates as an empty string which gets passed to your print function.

So, os.system isn't designed for what you are trying to achieve.

If you potentially have other folders in the parent folder you are searching, that may also have the filenames you are looking for, then I would recommend using the glob module.

import glob    

files = glob.glob("/media/radamand/230_GB/*mkv")  # Returns a list of strings for matched files
    for file in files:
        print(file[:-3])

You can add and set the keyword arguments recursive and/or include_hidden to True if required.

If, however, you are only looking for the files in the current folder, you can use fnmatch:

import fnmatch
import os

for file in os.listdir("/media/radamand/230_GB"):
    if fnmatch.fnmatch(file, "*mkv"):
        print(file[:-3])
bigkeefer
  • 576
  • 1
  • 6
  • 13