# Plex Auto-Delete Script

**URL:** <https://forums.plex.tv/t/plex-auto-delete-script/45826>\
**Category:** Dev/API Corner\
**Tags:** other-dev\
**Created:** [October 31, 2013, 6:36pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826 "2013-10-31T18:36:27Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![doughnet](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@doughnet](https://forums.plex.tv/u/doughnet)\
**Post date:** [January 1, 2014, 12:33am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/41 "2014-01-01T00:33:05Z")

</div>

cool idea

how about integrating a way to have it delete the oldest file for movies? &nbsp;example, a movie that has never been watched over 90 days = deleted.

i'd like that.

---

<div class="post-metadata">

**Author:** ![Steven4x4](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Steven4x4](https://forums.plex.tv/u/Steven4x4)\
**Post date:** [January 1, 2014, 12:44am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/42 "2014-01-01T00:44:17Z")

</div>

> cool idea
> 
> how about integrating a way to have it delete the oldest file for movies? &nbsp;example, a movie that has never been watched over 90 days = deleted.
> 
> i'd like that.

That would actually be quite easy to do.

So you want it to simply look through all of your movie files and DELETE if its older than 90 days? (What it has been watched, do you want to keep those or delete thoses?)

That can actually be done with a much simpler script, assuming that you put your movies in a different directory from your TV shows.

Are you on Windows, Linux or MAC?

I see in your Signature you have a San Jose Server. What company is that hosted through? What kind of pricing are you paying on that if you don't mind me asking?

---

<div class="post-metadata">

**Author:** ![STEEVo](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/steevo/32/90387_2.png) [@STEEVo](https://forums.plex.tv/u/STEEVo)\
**Post date:** [January 1, 2014, 8:05am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/43 "2014-01-01T08:05:19Z")

</div>

> I'm sure there is a key identifier, but haven't had the time to clearly look through and make the distinction of how Plex is associating their ViewShows attributes to the attributes in there OnDeck.

  
  
Thank you for explaining your logic to me. I don't think like a programmer. I went back through the xml and I think I found the attributes that we need (and my best guess is the same attributes that Plex uses.) The _recentlyViewed_ xml contains the season and episode attributes for each file! They are these two tags:  
&nbsp;
```
index="2" parentIndex="1"
```

Translated: Season 1 (parentIndex="1") episode 2 (index="2")

```auto

<br><br>
Would this make a clean solution? for example [for each series: protect the largest index=value from deletion | allow deletion of all remaining files] :o<br><br>
Edit: Or... Alternatively, you could possibly use the OnDeck file attributes to do the same thing in reverse: [for each series: protect from deletion files that match the series and have an index=value of index=value - 1 for each onDeck file]<br>
&nbsp;</p>

<blockquote class="ipsBlockquote" data-author="Steven4x4" data-cid="537989" data-time="1388532306"><br>
My understanding was thatPlex already did the hard work after you watched the show it changed the attributes on the episodes and identified which one was OnDeck, (Since it Clearly has a Specific Section for OnDeck) so that means it should have already been marked and not dependent on other files, but its seems thats not the case.</blockquote>
<p>
&nbsp;</p>
<p>Yes, I believe you are exactly right. Plex does the work and marks the upcoming unwatched episode as onDeck. However, that tag doesn't stay very long. Plex removes it rather quickly (on next library scan.) During the library scan it finds that there are no longer watched episodes for that series in the library (because all watched episodes were deleted by this script.) Plex then removes the tag. The OnDeck tag is entirely dependent on the last watched episode remaining in the library.</p>
```

---

<div class="post-metadata">

**Author:** ![STEEVo](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/steevo/32/90387_2.png) [@STEEVo](https://forums.plex.tv/u/STEEVo)\
**Post date:** [January 9, 2014, 4:20am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/44 "2014-01-09T04:20:43Z")

</div>

egretsareherons & Steven4x4

I tested the latest version of the script on mac. It works great. I have noticed one issue:

These files failed:&nbsp;

Hawaii Five-0 - 4x09 - Hau’oli Lā Ho’omaika’i.mkv

```
1 views: /Volumes/ext3TB/TV/Hawaii Five-0/Season 04/Hawaii Five-0 - 4x09 - Hau%E2%80%99oli L%C4%81 Ho%E2%80%99omaika%E2%80%99i.mkv
##[NOT FOUND] /Volumes/ext3TB/TV/Hawaii Five-0/Season 04/Hawaii Five-0 - 4x09 - Hau%E2%80%99oli L%C4%81 Ho%E2%80%99omaika%E2%80%99i.mkv
```

Raising Hope - 4x01 - Déja Vu Man.mkv

&nbsp;

```
1 views: /Volumes/ext3TB/TV/Raising Hope/Season 04/Raising Hope - 4x01 - D%C3%A9ja Vu Man.mkv
##[NOT FOUND] /Volumes/ext3TB/TV/Raising Hope/Season 04/Raising Hope - 4x01 - D%C3%A9ja Vu Man.mkv
```

It seems that special characters like (é &&nbsp;ā in this situation) are causing the issue. You probably have the answer/fix for this... My guess is that the data types used are incompatible with those characters.

Thanks again for the script!

Edit: Potentially helpful resource for solving:&nbsp;[http://www.reddit.com/r/learnpython/comments/178juj/trouble\_with\_accent\_characters/](http://www.reddit.com/r/learnpython/comments/178juj/trouble_with_accent_characters/)

---

<div class="post-metadata">

**Author:** ![egretsareherons](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@egretsareherons](https://forums.plex.tv/u/egretsareherons)\
**Post date:** [January 9, 2014, 7:24am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/45 "2014-01-09T07:24:47Z")

</div>

&nbsp;

> egretsareherons & Steven4x4
> 
> I tested the latest version of the script on mac. It works great. I have noticed one issue:
> 
> These files failed:&nbsp;
> 
> Hawaii Five-0 - 4x09 - Hau’oli Lā Ho’omaika’i.mkv
> 
> &nbsp;

Hey STEEVo,

The XML coming from plex has percent-encoded unicode. &nbsp;I have a fix for ya. &nbsp;

Search for this, (around line 300 of my version):

```
  ####################################################################################
  ##  Get Files for Watched Shows
  ####################################################################################
  changed = 0
  for VideoNode in doc.getElementsByTagName("Video"):
    view = VideoNode.getAttribute("viewCount")
    if view == '':
      view = 0
    view = int(view)
    MediaNode = VideoNode.getElementsByTagName("Media")
    for Media in MediaNode:
      PartNode = Media.getElementsByTagName("Part")
      for Part in PartNode:
        file = Part.getAttribute("file")
        print str(view) + " views: " + file

&nbsp; &nbsp; &nbsp; &nbsp; #NEW:  
&nbsp; &nbsp; &nbsp; &nbsp; if PC == ‘L’:  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file = urllib2.unquote(file.encode(‘utf-8’)).decode(‘utf-8’)  
&nbsp; &nbsp; &nbsp; &nbsp; else:  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; import urllib  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file = urllib.unquote(file.encode(‘utf-8’)).decode(‘utf-8’)
```

The lines under "new" are the ones you want to add. This is python, so whitespace matters. &nbsp;Make sure you use spaces (not tabs) and line everything up.

That windows fix (in the else) is untested & it's kinda ugly, seeing as how I'm importing a library there. It's just a kludge. &nbsp;Steven4x4 imports only urllib.request above, and I'm not sure why. &nbsp;I'm sure he has a good reason, but not being much of a Windows programmer, I don't know the intricacies of that sort of thing. It will work for now, but should probably be cleaned up a bit in a later version.

---

<div class="post-metadata">

**Author:** ![STEEVo](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/steevo/32/90387_2.png) [@STEEVo](https://forums.plex.tv/u/STEEVo)\
**Post date:** [January 11, 2014, 12:11am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/46 "2014-01-11T00:11:44Z")

</div>

Thanks egretsareherons! I am not sure if it works or not yet because I deleted the files that had the accents. However, I added it and will let you know as soon as another file shows up!&nbsp;

I thought of another feature to add to the list. The leftover metadata files are piling up (not deleted with the video files.) Having an option to delete metadata files by extension would be great! You could just enable a list of extensions to delete that match the video file name. In my case I would want to delete .NFO & .TBN I am sure some people would have others or more.

Thanks again!&nbsp; :)

---

<div class="post-metadata">

**Author:** ![eskwire](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/eskwire/32/3842_2.png) [@eskwire](https://forums.plex.tv/u/eskwire)\
**Post date:** [January 15, 2014, 5:52am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/47 "2014-01-15T05:52:40Z")

</div>

It would be cool that they include this in the PHT whit a FUI whit just a few click and don.

BTW nice job.&nbsp;

There any way that when he delete he send the files to the trash can instead of permanently delete them?

---

<div class="post-metadata">

**Author:** ![aross01](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@aross01](https://forums.plex.tv/u/aross01)\
**Post date:** [January 29, 2014, 8:54pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/48 "2014-01-29T20:54:08Z")

</div>

Steven Johnson

&nbsp;

This is what I have been looking for and also a thanks to

&nbsp; egretsareherons as I just added to his v2 to make a v3

&nbsp;&nbsp;

The only thing I wanted to add was a number of days to check

to make sure the video was not deleted early there are a number of ways

the video might get marked as watched like you go to sleep and&nbsp;

the video plays to the end, or someone else may watch the video&nbsp;

before you get a chance to.

&nbsp;

I made a version v3&nbsp;

DaysToKeep ( check number of days since added do not deleted early)

&nbsp;

DaysToKeep = 14

&nbsp;

&nbsp; &nbsp; ################################################################

&nbsp; &nbsp; ###Find number of days between date video was added and today

&nbsp; &nbsp; addedAt &nbsp; &nbsp; &nbsp;= VideoNode.getAttribute("addedAt")

&nbsp; &nbsp; d1 = datetime.datetime.today()

&nbsp; &nbsp; d2 = datetime.datetime.fromtimestamp(float(addedAt))

&nbsp; &nbsp; DaysSincevideoAdded = (d1 - &nbsp; d2).days

&nbsp; &nbsp; ################################################################

&nbsp;

&nbsp;

## -- Check if video older than days to keep --

&nbsp; if CantDelete == 0:

&nbsp; &nbsp; if DaysToKeep \> DaysSincevideoAdded:

&nbsp; &nbsp; &nbsp; CantDelete += 1

&nbsp; &nbsp; &nbsp; ShowFound = " [Days:" &nbsp;+ str(DaysSincevideoAdded) +"]" + ShowFound

&nbsp; &nbsp; &nbsp; ShowsCount += 1

&nbsp; else:

&nbsp; &nbsp; if DaysToKeep \> DaysSincevideoAdded:

&nbsp; &nbsp; &nbsp; CantDelete += 1

&nbsp; &nbsp; &nbsp; ShowFound = " [Days:" &nbsp;+ str(DaysSincevideoAdded) +"]" + ShowFound

&nbsp;

What I would like to add is a number of days per show, and maybe

a flag to not count weekends to handle a daily show mon-fri.

&nbsp;

Or maybe number of episode&nbsp;to save per show.

---

<div class="post-metadata">

**Author:** ![eskwire](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/eskwire/32/3842_2.png) [@eskwire](https://forums.plex.tv/u/eskwire)\
**Post date:** [January 29, 2014, 10:59pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/49 "2014-01-29T22:59:51Z")

</div>

I know might be you already add this to it but it would be cool on the TV show section that he doesn't delete the last two episode, keep the two last one like reference. And some movies and Tv show specially in HD come in folder, i see it delete only the file.

Thanks.

---

<div class="post-metadata">

**Author:** ![akabdog](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/akabdog/32/9806_2.png) [@akabdog](https://forums.plex.tv/u/akabdog)\
**Post date:** [February 23, 2014, 9:28am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/50 "2014-02-23T09:28:06Z")

</div>

much thanks to everyone who worked on this, I just used v3 and it worked great.

here is what it looked like for me:

--

PC = ""

Host = ""

Port = ""

SectionList = []

IgnoreSections = []

Delete = "0"

Move = "1"

Copy = "0"

Shows = [""]

OnDeck = ""

DestinationDir = "/t/trash"

DestinationSection = ""

TriggerRescan = False

DaysToKeep = 0

--

----------------------------------------------------------------------------

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Summary -- Script Completed Successfully

----------------------------------------------------------------------------

&nbsp;

&nbsp; Total File Count &nbsp; &nbsp; &nbsp;117

&nbsp; Kept Show Files &nbsp; &nbsp; &nbsp; 0

&nbsp; On Deck Files &nbsp; &nbsp; &nbsp; &nbsp; 0

&nbsp; Deleted Files &nbsp; &nbsp; &nbsp; &nbsp; 0

&nbsp; Moved Files &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 117

&nbsp; Copied Files &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0

&nbsp; Flagged Files &nbsp; &nbsp; &nbsp; &nbsp; 0

&nbsp; Rescanned Sections&nbsp;

----------------------------------------------------------------------------

$ du -sh /t/trash

94G

---

<div class="post-metadata">

**Author:** ![natew](https://avatars.discourse-cdn.com/v4/letter/n/8dc957/32.png) [@natew](https://forums.plex.tv/u/natew)\
**Post date:** [February 26, 2014, 11:23pm UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/51 "2014-02-26T23:23:58Z")

</div>

Is there an easy way to modify this to only delete a few specific shows? I think, if I reading this right, the logic is the other way around were it will delete every except what you shows you specify.

Hopefully that makes sense and great work Steven.

---

<div class="post-metadata">

**Author:** ![Steven4x4](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Steven4x4](https://forums.plex.tv/u/Steven4x4)\
**Post date:** [February 27, 2014, 12:14am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/52 "2014-02-27T00:14:24Z")

</div>

> Is there an easy way to modify this to only delete a few specific shows? I think, if I reading this right, the logic is the other way around were it will delete every except what you shows you specify.
> 
> Hopefully that makes sense and great work Steven.

That is correct.

However, The Code is simple enough that it can be re-versed fair easily.

Unfortunately, I haven't had time to work on this project in several months, however there are few guys in here that have made some updates and changes that might be able to chime in with the solution much quicker than I.

So I will see if I can come up with the answer for you...

---

<div class="post-metadata">

**Author:** ![Steven4x4](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Steven4x4](https://forums.plex.tv/u/Steven4x4)\
**Post date:** [February 27, 2014, 12:25am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/53 "2014-02-27T00:25:23Z")

</div>

Few Sections Require Changes:

For the Header Information Change this part to: (Really Is Just The Last 2 Lines For Informational Purposes)

```
####################################################################################
## Checking Shows
####################################################################################
NoDelete = " | "
ShowCount = len(Shows)
print("Show Count: " + str(ShowCount))

for Show in Shows:  
Show = re.sub(’[^A-Za-z0-9]+’, ‘’, Show).strip()  
if Show=="":  
NoDelete += "(None Listed) | "  
ShowCount -= 1  
else:  
NoDelete += Show + " | "

print("Number of Shows Detected For Deletion: " + str(ShowCount))  
print (“Shows to Delete:” + NoDelete)
```

THEN

```
####################################################################################
## Check Shows And Delete If Configured
####################################################################################
def CheckShows( CheckFile ):
  global FileCount
  global DeleteCount
  global FlaggedCount
  global OnDeckCount
  global ShowsCount
  FileCount += 1
  CantDelete = 0
  ShowFound = ""
## – CHECK SHOWS –

for Show in Shows:  
Show = re.sub(’[^A-Za-z0-9]+’, ‘’, Show).strip()  
if Show=="":  
CantDelete = 0  
else:  
if (’ ’ in Show) == True:  
if all(str(Word) in CheckFile for Word in Show.split()):  
CantDelete += 0  
ShowFound = “[” + Show + “]”  
ShowsCount += 1  
else:  
CantDelete += 1  
else:  
if Show in CheckFile:  
CantDelete += 0  
ShowFound = “[” + Show + “]”  
ShowsCount += 1  
else:  
CantDelete += 1

## – Check OnDeck –

if OnDeck==“1”:  
IsOnDeck = CheckOnDeck(CheckFile);  
if IsOnDeck==0:  
CantDelete += 0  
else:  
CantDelete += 1  
ShowFound = “[OnDeck]” + ShowFound  
OnDeckCount += 1

## – DELETE SHOWS –

if CantDelete \>= 1:  
if Delete==“1”:  
print("**[DELETED] " + CheckFile)  
os.remove(file)  
DeleteCount += 1  
else:  
print("**[FLAGGED] " + CheckFile)  
FlaggedCount += 1  
else:  
print("[KEEPING]" + ShowFound + " " + CheckFile)
```

I Believe those changes shoul do it...

FOR SAFETY REASONS: (AND THE LACK OF TESTING)

BACKUP YOUR DATA FIRST (BEFORE RUNNING)

SECONDLY Make Sure DELETE = "0" in the top settings to make sure that its only informational and not actually deleting anything...

Hope this works for you..

SIDE NOTE: You may have to remove in the 2nd code box I posted the small sub section regaring OnDeck as this might mess up what you are tryting to do..

(YOU MAY or MAY NOT NEED TO REMOVE THE BELOW SECTION

```
## -- Check OnDeck --
  if OnDeck=="1":
    IsOnDeck = CheckOnDeck(CheckFile);
    if IsOnDeck==0:
      CantDelete += 0
    else:
      CantDelete += 1
      ShowFound = "[OnDeck]" + ShowFound
      OnDeckCount += 1
```

---

<div class="post-metadata">

**Author:** ![STEEVo](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/steevo/32/90387_2.png) [@STEEVo](https://forums.plex.tv/u/STEEVo)\
**Post date:** [February 27, 2014, 5:59am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/54 "2014-02-27T05:59:27Z")

</div>

I made a couple updates if anyone is interested:

- Added an additional keep mode based on last viewed date (rather than the added date.)
- Added the toggle to choose between the two
- Simplified the logging formatting a bit and added additional logging for the above additions

Edit: [Updated/Fixed Code Here](https://forums.plex.tv/topic/84822-plex-auto-delete-script/#entry589032)

---

<div class="post-metadata">

**Author:** ![STEEVo](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/steevo/32/90387_2.png) [@STEEVo](https://forums.plex.tv/u/STEEVo)\
**Post date:** [February 27, 2014, 6:49am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/55 "2014-02-27T06:49:34Z")

</div>

Thanks again Steven4x4 and the rest who have contributed!

If anyone has any helpful pointers I am still having trouble with files that use special characters:

```
Filename:
Hawaii Five-0 - 4x14 - Nā Hala a Ka Makua - 720p.mkv

Plex’s XML gives this:  
Hawaii Five-0 - 4x14 - N%C4%81 Hala a Ka Makua - 720p.mkv
```

Of course the script then fails to find/delete the file...

Here is what I have been able to figure out

The **%C4%81** portion of the filename in the XML needs to have the HTML entities stripped/replaced and corrected to this: **\xC4\x81&nbsp;** which can then be correctly converted back into the proper filename before file deletion.

Can anyone help me with code to do this to any filenames with these special characters? Not a big deal if not. I can just manually delete them.

in python I enter this: and get the correct output:

```
>>> print '\xC4\x81'
ā
```

---

<div class="post-metadata">

**Author:** ![egretsareherons](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@egretsareherons](https://forums.plex.tv/u/egretsareherons)\
**Post date:** [February 27, 2014, 6:53am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/56 "2014-02-27T06:53:27Z")

</div>

> Thanks again Steven4x4 and the rest who have contributed!
> 
> If anyone has any helpful pointers I am still having trouble with files that use special characters:
> 
> ```
> Filename:
> Hawaii Five-0 - 4x14 - Nā Hala a Ka Makua - 720p.mkv
> 
> Plex’s XML gives this:  
> Hawaii Five-0 - 4x14 - N%C4%81 Hala a Ka Makua - 720p.mkv
> ```
> 
> Of course the script then fails to find/delete the file...
> 
> Here is what I have been able to figure out
> 
> The **%C4%81** portion of the filename in the XML needs to have the HTML entities stripped/replaced and corrected to this: **\xC4\x81&nbsp;** which can then be correctly converted back into the proper filename before file deletion.
> 
> Can anyone help me with code to do this to any filenames with these special characters? Not a big deal if not. I can just manually delete them.
> 
> in python I enter this: and get the correct output:
> 
> ```
> >>> print '\xC4\x81'
> ā
> ```

Did my above fix (comment #45) not work for you, Steevo? &nbsp;I tested it on Linux & it worked great. &nbsp;

[https://forums.plex.tv/topic/84822-plex-auto-delete-script/page-3#entry547782](https://forums.plex.tv/topic/84822-plex-auto-delete-script/page-3#entry547782)

---

<div class="post-metadata">

**Author:** ![STEEVo](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/steevo/32/90387_2.png) [@STEEVo](https://forums.plex.tv/u/STEEVo)\
**Post date:** [February 27, 2014, 6:58am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/57 "2014-02-27T06:58:29Z")

</div>

> Did my above fix (comment #45) not work for you, Steevo? &nbsp;I tested it on Linux & it worked great. &nbsp;
> 
> [https://forums.plex.tv/topic/84822-plex-auto-delete-script/page-3#entry547782](https://forums.plex.tv/topic/84822-plex-auto-delete-script/page-3#entry547782)

It did not work for me unfortunately. I think it would however if I could reformat the filename retrieved from XML from&nbsp;%C4%81 to&nbsp;'\xC4\x81' (from example above) first. Did you it work for you with a filename with those % entities? Thanks again for the help!

EDIT: I see what you mean. My terminal output:

```
>>> import urllib2
>>> target = '%C4%81'
>>> print urllib2.unquote(target)
ā
```

It should work. Let me try again

---

<div class="post-metadata">

**Author:** ![STEEVo](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/steevo/32/90387_2.png) [@STEEVo](https://forums.plex.tv/u/STEEVo)\
**Post date:** [February 27, 2014, 7:32am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/58 "2014-02-27T07:32:18Z")

</div>

@egretsareherons

I think it works but then it causes this error on the next print command that includes 'file':

```
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0101' in position 112: ordinal not in range(128)
```

Errors on this line (in my script):

```
print "Viewed:" + str(view) + "x | Days Since Viewed: " + str(DaysSinceVideoLastViewed) + " Added: " + str(DaysSincevideoAdded) + " | " + file
```

---

<div class="post-metadata">

**Author:** ![egretsareherons](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@egretsareherons](https://forums.plex.tv/u/egretsareherons)\
**Post date:** [February 27, 2014, 7:33am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/59 "2014-02-27T07:33:19Z")

</div>

Well, some of this is probably going to come down to how the os & shutil modules handle escaped characters. &nbsp;Those are somewhat OS specific. &nbsp;On linux and presumably on osx, it would not work to remove the \x style encoding. &nbsp;What the urllib.unquote function does (urllib2.unquote(file.encode('utf-8')).decode('utf-8'))&nbsp;is take the %encoded XML and translate it back to the goofy looking (ie&nbsp;ā)&nbsp;character. &nbsp;So it's using the actual system filename when going to remove the file.

I'm moving my files, not deleting them. IIRC you are removing your files, right? &nbsp;I see that the remove command is os.remove, while the move and copy are called from shutil. &nbsp;There could be a difference in unicode handling between those two modules. &nbsp;(If you haven't yet realized this, unicode is a giant pain the ass in python 2.x.) &nbsp;That is to say, os.remove() may be balking at the raw unicode character. &nbsp;(I really have no idea, I'm just guessing here.)&nbsp;

One thing you could try, as a test, is using my fix with the copy or move function. &nbsp;If that works, it's likely that its os.remove() that's having the problem. &nbsp;In that case, you could convert the os.remove() call to a shutil.rmtree() call, though that's a little dangerous looking, since it has the potential to remove a whole directory tree. &nbsp;You'd wanna be real careful there. &nbsp;Another solution would be to use shutil.move() to move the unicode containing filename to a non-unicode encoded name, then remove that file using os.remove().

All of that sounds pretty hacky, I know. &nbsp;I would have expected the original fix to work on all os's, but&nbsp;Unicode is not my strong suit. &nbsp;

---

<div class="post-metadata">

**Author:** ![egretsareherons](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@egretsareherons](https://forums.plex.tv/u/egretsareherons)\
**Post date:** [February 27, 2014, 7:36am UTC](https://forums.plex.tv/t/plex-auto-delete-script/45826/60 "2014-02-27T07:36:11Z")

</div>

> @egretsareherons
> 
> I think it works but then it causes this error on the next print command that includes 'file':
> 
> ```
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u0101' in position 112: ordinal not in range(128)
> ```
> 
> Errors on this line (in my script):
> 
> ```
> print "Viewed:" + str(view) + "x | Days Since Viewed: " + str(DaysSinceVideoLastViewed) + " Added: " + str(DaysSincevideoAdded) + " | " + file
> ```

OK! &nbsp;So it is working, it's just the print command that's failing. That's good!

So that means that we just have to make the file thingy print properly.

Try this: &nbsp;

print "Viewed:" + str(view) + "x | Days Since Viewed: " + str(DaysSinceVideoLastViewed) + " Added: " + str(DaysSincevideoAdded) + " | " + file.encode('utf-8').decode('utf-8')

**Edit:&nbsp;** If that encode() decode thing doesn't work, in the meantime, you can wrap the whole thing in a try/except block. &nbsp;That way the whole script won't barf on one error. &nbsp;So like this: &nbsp;

```
try:
  print "Viewed:" + str(view) + "x | Days Since Viewed: " + str(DaysSinceVideoLastViewed) + " Added: " + str(DaysSincevideoAdded) + " | " + file 

except Exception, e:  
&nbsp; print “error printing unicode character”
```

It doesn't fix the problem per se, but it leaves you with a working script until someone smarter than me can tell you how to get it to print properly. &nbsp;

Man, I hate unicode.

[Previous page](https://forums.plex.tv/t/plex-auto-delete-script/45826.md?page=2)

[Next page](https://forums.plex.tv/t/plex-auto-delete-script/45826.md?page=4)
