Reputation: 45
I have a directory full of playlist files and each play list looks like this:
#EXTM3U
#EXTINF:116,VME MidSeal
V:\Eagle Presentation\VME_MidSeal.wmv
#EXTINF:0,IMG 1061
\Users\cla5598\Pictures\house\IMG_1061.JPG
#EXTINF:0,IMG 1062
\Users\cla5598\Pictures\house\IMG_1062.JPG
#EXTINF:0,IMG 1067
\Users\cla5598\Pictures\house\Directory\IMG_1067.JPG
#EXTINF:0,IMG 1068
First I need to Replace the directory with a new directory ... for example
\Users\cla5598\Pictures\house\ change to /New/Pictures/
Then I need to change any other "\" to "/" because there are other sub directory's
So far I have gotten the first part to work great.. but I'm having issues with the "\" to "/" part....
so far I have this and I'm getting an error with the last part:
import os
import fileinput
file_list=[]
for file in os.listdir("D:\Users\cla5598\Documents\Aptana Studio 3 Workspace\Playlist_Convert"):
if file.endswith(".m3u8"):
file_list.append(file)
print file_list
for i in file_list:
f1 = open(i, "r")
f2 = open("D:\Users\cla5598\Documents\Aptana Studio 3 Workspace\Playlist_Convert\\new\\" + "Pi-" + i, "w+")
for line in f1:
f2.write(line.replace("\Users\cla5598\Pictures\house\\", "/New/Pictures/"))
f1.close()
f2.close()
file_list_new=[]
for file in os.listdir("D:\Users\cla5598\Documents\Aptana Studio 3 Workspace\Playlist_Convert\\new"):
if file.endswith(".m3u8"):
file_list_new.append(file)
print file_list_new
for b in file_list_new:
f3 = fileinput.input(b, inplace=1)
for line in f3:
line.replace("\\", "/")
f3.close()
Here is the error I'm getting:
['list1.m3u8', 'list2.m3u8']
['Pi-list1.m3u8', 'Pi-list2.m3u8']
Traceback (most recent call last):
File "D:\Users\cla5598\Documents\Aptana Studio 3 Workspace\Playlist_Convert\Playlist_Convert.py", line 30, in <module>
for line in f3:
File "C:\Python27\lib\fileinput.py", line 252, in next
line = self.readline()
File "C:\Python27\lib\fileinput.py", line 321, in readline
os.rename(self._filename, self._backupfilename)
WindowsError: [Error 2] The system cannot find the file specified
Upvotes: 0
Views: 114
Reputation: 2773
First of all, your error is that the file you're trying to read does not exist. The reason is that your current directory (The directory from which you run the code) is probably D:\Users\cla5598\Documents\Aptana Studio 3 Workspace\Playlist_Convert
. But os.listdir
returns only file names. In the second time you list the directory D:\Users\cla5598\Documents\Aptana Studio 3 Workspace\Playlist_Convert\\new
which is not your current directory. So the script tries to read files that doesn't exist. To fix this you should use full paths like this:
os.path.join(r"D:\Users\cla5598\Documents\Aptana Studio 3 Workspace\Playlist_Convert\new", b)
Or you can use paths relative to your current directory:
os.path.join(r"new", b)
There is another problem in your code.
From the documentation:
if the keyword argument inplace=1 is passed to fileinput.input() or to the FileInput constructor, the file is moved to a backup file and standard output is directed to the input file
Try running your code in its current shape (Backup all the files first!). You'll see it adds the character '
around each line. This happens because when you type in the console:
a = "hi"
a
Then you get 'hi'
- your string inside '
. And since the standard output (the output to the console) is directed to your file, this is what happens.
So what you should do is instead of line.replace("\\", "/")
do:
print line.replace("\\", "/"),
Notice the "," at the end - this is so that a new line won't be added.
And one last thing: I think this way is the wrong way to do it since there is no reason to direct standard output to your file and since using a sophisticated library is an over-kill in my opinion.
I think you should do it the same way you did the first part. Just write your output to a new file. Also, this is not relevant to your question at all, but you may want to look at how to use the with
statement in python. This way you can use with open("some-file") as f: do-things()
and python will close the file for you.
EDIT:
You can combine the loops and do part 1 and part 2 together:
file.write(line.replace("\Users\cla5598\Pictures\house\\", "/New/Pictures/").replace("\\", "/"))
And you can do:
for i in os.listdir("D:\Users\cla5598\Documents\Aptana Studio 3 Workspace\Playlist_Convert"):
if file.endswith(".m3u8"):
And delete the first loop.
Upvotes: 1