Python Forum
Python with win32com and EXIF renaming files.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python with win32com and EXIF renaming files.
#1
Current behavior: blinking cursor

Expected behavior: renaming media files.

Steps I took to make this project
1. First I use pathlib to iterate for each file in the current folder.

2. Second I check if the file suffix on capital letters I convert it to
small.

3. Third I check if the file suffix is '.m4v'.

4. Fourth If the file suffix is .m4v I use win32com to get the Media
Creation date and time

5. Fifth I modify the date and time to fit the windows naming system.

6. Finally I use os.rename to rename any file that it's extension equal
.m4v with the t value which is in fifth step.

Everything works but renaming is not!

printing the the original files name works.

printing the media creation date using win32com works fine.

printing random text before the renaming line works.

printing random text after renaming line doesn't work.



here is the code:
```py

import os
import pathlib
from PIL import Image
from PIL.ExifTags import TAGS
# import datetime
from win32com.propsys import propsys, pscon
import time
from datetime import datetime


for p in pathlib.Path.cwd().iterdir():
    ext = p.suffix
    tl = ext.lower()
    
    if p.suffix == '.m4v':
        vid = p.stem + p.suffix
        
        properties = propsys.SHGetPropertyStoreFromParsingName(str(p))

        dt = properties.GetValue(pscon.PKEY_Media_DateEncoded).GetValue()
        
        t = datetime.strftime(dt, '%Y%m%d')
        
        print(dt)

        os.rename(vid , t + p.suffix)

        mov_name = p.stem
```
Reply
#2
cross-posted on StackOverflow
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
You need to show the output and demonstrate what you mean by "random text", and indicate what was expected vs. what was shown. There is nothing in the code sample that helps analyze what is going on.
Reply
#4
(Apr-03-2021, 02:40 AM)supuflounder Wrote: You need to show the output and demonstrate what you mean by "random text", and indicate what was expected vs. what was shown. There is nothing in the code sample that helps analyze what is going on.

I've already mentioned at the beginning of the post that the current behavior is a blinking cursor.
So, the random text means a random string to test if the os.name works without the EXIF value.

If you read the code you will find what should be the expected result, but it seems that you read it but not willing to understand.

It's very basic code nothing is complex about it that may not be clear, but already I explained what is the steps I took to write this code and it's in the post.
Reply
#5
(Apr-02-2021, 07:01 PM)Amrcodes Wrote: os.rename(vid , t + p.suffix)

I don't understand why you work with pathlib, if you still do the low-level-stuff by yourself.
For example, Path has a rename method from the beginning on.
The t + p.suffix is just a workaround because we had not the method with_stem. Since Python 3.9 it was introduced.

vid = Path("/tmp/foo/bar/a_file.m4v")
print(vid)

new_file = vid.with_stem("2021-03-04T22:42:00")
# with_stem was introduced with Python 3.9.
print(new_file)

vid.rename(new_file)
We don't care about blinking cursors, we want input (which is your output).
Run your code and show the output. If your code does not show output, use the print-function.
If it fails, show also the whole traceback.

The use of print(repr(some_object)) can clarify which kind of object this is.
repr is a built-in function, which shows the representation of an object as str.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 357 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,564 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  Functions to consider for file renaming and moving around directories cubangt 2 1,770 Jan-07-2022, 02:16 PM
Last Post: cubangt
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,502 Oct-21-2021, 03:29 AM
Last Post: buran
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,252 Aug-18-2021, 05:27 AM
Last Post: snippsat
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,903 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  Python win32com.client: What are the syntax to open exe file & activate its window? JaneTan 0 4,216 Oct-14-2020, 09:09 AM
Last Post: JaneTan
  Alternate package of win32com.client manigandanpro 0 3,778 Aug-19-2020, 07:38 AM
Last Post: manigandanpro
  copying an Excelsheet with its conent and renaming into different names deheugden 6 3,253 Jun-05-2020, 03:20 PM
Last Post: deheugden
  renaming the file in a suitable format, I just wondering if it is possible go127a 11 5,182 Jun-26-2019, 06:15 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020