0

To the point: In a python script with YAML syntax I want to rename a file using:

{created.year}-{created.month:02}-{created.day:02}_{path.stem}.jpg

Would it be possible to remove the first n characters of path_stem? If so, how? Thank you!

  • Not sure what you mean with the YAML syntax stuff. In general `path.stem` simply returns a string, which you can always edit like this: https://stackoverflow.com/a/11806564/4026792 – Thomas Dec 05 '20 at 12:04
  • Thanks. My question was how to remove the first n characters of that since I am a Python novice? In Bash one does mv "$file" "${file%???????}" to remove the first 7 characters. Is there an analog with Python and pathlib? :) Thanks! – Emmanuel Goldstein Dec 06 '20 at 15:15
  • That should be explained in the answer I linked, in your example that would be `path.stem[n:]` to remove the first n characters. – Thomas Dec 06 '20 at 20:05
  • Does this answer your question? [Removing first x characters from string?](https://stackoverflow.com/questions/11806559/removing-first-x-characters-from-string) – Thomas Dec 06 '20 at 20:05
  • When using path.stem[7:] I get the error "ERROR! Invalid format specifier" for the colon. When I remove the colon, then I only get the 7th character of the name. – Emmanuel Goldstein Dec 07 '20 at 07:16
  • Could you explain more about in which context you are using this script? You said something about YAML syntax, why do you mean by that? It doesn't look like standard python. PS: It's very confusing that the other commenter is also named Thomas ;) – Thomas Dec 08 '20 at 09:56
  • Look at this: https://snipboard.io/TQgPdv.jpg It's from the organice tool: https://organize.readthedocs.io/en/latest/page/actions.html#python. The you will have an idea of what I want to do :) – Emmanuel Goldstein Dec 08 '20 at 18:37

0 Answers0