简体   繁体   中英

Python Reading a text file

I wrote an applescript to generate a text file of all unwatched movies in my itunes library. Here is the code of that script:

tell application "iTunes"
    set watchedEpisodes to tracks of playlist "TV Shows" whose unplayed is false and played count > 0
    set unwatchedEpisodes to tracks of playlist "TV Shows" whose unplayed is true
    if (count unwatchedEpisodes) > 0 then
        set trackNames to {}
        repeat with anEpisode in unwatchedEpisodes
            set tmp to location of anEpisode as text
            set posixtmp to POSIX path of tmp
            set end of trackNames to (posixtmp as text)
        end repeat

        set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}

        set unwatchedTVShows to trackNames as text
        do shell script "echo " & quoted form of unwatchedTVShows & " > /Volumes/Data/Media/Kodi/unwatchedTVShows.txt"
        #display dialog trackNames as text
        set AppleScript's text item delimiters to TID
    end if

    set watchedEpisodes to tracks of playlist "Movies" whose unplayed is false and played count > 0
    set unwatchedEpisodes to tracks of playlist "Movies" whose unplayed is true
    if (count unwatchedEpisodes) > 0 then
        set trackNames to {}
        repeat with anEpisode in unwatchedEpisodes
            set tmp to location of anEpisode as text
            set posixtmp to POSIX path of tmp
            set end of trackNames to (posixtmp as text)

        end repeat

        set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}

        set unwatchedMovies to trackNames as text
        do shell script "echo " & quoted form of unwatchedMovies & " > /Volumes/Data/Media/Kodi/unwatchedMovies.txt"
        #display dialog trackNames as text
        set AppleScript's text item delimiters to TID
    end if
end tell

Here is my output file:

/Volumes/Data/Media/Movies/Thanks For Sharing.m4v
/Volumes/Data/Media/Movies/Thats My Boy.m4v
/Volumes/Data/Media/Movies/Think Like a Man.m4v
/Volumes/Data/Media/Movies/Think Like a Man Too.m4v
/Volumes/Data/Media/Movies/This Means War.m4v
/Volumes/Data/Media/Movies/Top Five.m4v
/Volumes/Data/Media/Movies/Trail of Blood.m4v

My problem is reading this file. I need to open it, and put it in a string so that I can search it wholeistically (ie, if I search for "Think Like a Man" I don't want a false positive on "Think Like a Man Too".

Anyway, here is my current python code (or at least a snippet of that code):

KODIFOLDER="/Volumes/Data/Media/Kodi"
with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as umovies:
    for line in umovies:
        line = line.strip()
        print line

Here is the output:

/Volumes/Data/Media/Movies/Trail of Blood.m4voo.m4v

Clearly, it's opening and reading the file, but I'm not understanding why the snippet isn't providing a full "list" of what's in the text file. Any thoughts as to what I'm doing wrong?

Here is the final answer. Not sure exactly what's different than what I had before (mixing tabs and spaces maybe??). Anyway, for future reference, this code:

with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as uwmovies:
    for line in uwmovies:
        line = line.replace("\n","")
        unwatchedmovies = line.split('\r')
        print unwatchedmovies

Gave the output of this list:

['/Volumes/Data/Media/Movies/Table 19.m4v', '/Volumes/Data/Media/Movies/Term Life.m4v', '/Volumes/Data/Media/Movies/Thanks For Sharing.m4v', '/Volumes/Data/Media/Movies/Thats My Boy.m4v', '/Volumes/Data/Media/Movies/Think Like a Man.m4v', '/Volumes/Data/Media/Movies/Think Like a Man Too.m4v', '/Volumes/Data/Media/Movies/This Means War.m4v', '/Volumes/Data/Media/Movies/Top Five.m4v', '/Volumes/Data/Media/Movies/Trail of Blood.m4v']

which was exactly what I needed. I list with no "\\r" or "\\n"'s. Thanks for the help!

Is this your actual code snippet?

KODIFOLDER="/Volumes/Data/Media/Kodi"
with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as umovies:
    for line in umovies:
        line = line.strip()
    print line

This would only print the last line of your file. Note, print line is only indented one tab .

Try calling .read() on the file you open and then split the content up on \\n (newline)

KODIFOLDER="/Volumes/Data/Media/Kodi"
with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as umovies:
    for line in umovies.read().split('\n'):
        line = line.strip()
        print line

But this is probably enough for your usecase:

KODIFOLDER="/Volumes/Data/Media/Kodi"
with open(KODIFOLDER + "/unwatchedMovies.txt", 'r') as umovies:
    umovies_content = umovies.read()
    print umovies_content 

Try This ( Pythonic Way , UPDATED ) :

import sys

read_lines = [sys.stdout.write(line.rstrip('\n') + "\n") for line in open('1.txt')]

Python 2x

from __future__ import print_function
read_lines = [print(line.rstrip('\n')) for line in open('1.txt')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM