Open a local file using python script?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
jachin99
Experienced User
Posts: 497
Joined: Sat Feb 13, 2016 8:39 pm

Open a local file using python script?

Post by jachin99 » Wed Oct 11, 2017 2:21 pm

I'm trying to automate moving files across my network using EG and python scripts, and I thought I would start learning this by doing something simple like opening a file using python. My problem is I get errors using known good methods. I have been successful opening files that are located in my c:\python27 directory but when I try to open files on my desktop or test files that I have dropped directly on my c drive I get errors. What could I be doing wrong? I've tried import os, and using that method to open a file, and I've tried the open method built into python. Here is an error message

>>> os.startfile('c:\test.txt')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
WindowsError: [Error 2] The system cannot find the file specified: 'c:\test.txt'

eirik226
Experienced User
Posts: 142
Joined: Wed Nov 07, 2012 5:22 pm

Re: Open a local file using python script?

Post by eirik226 » Wed Oct 11, 2017 2:47 pm

I can't really recall this but I think your path for the file is wrong. c:\\ maybe. I can't remember. Should be easy enough with a google search tho.

Edit: Post the script you have so far.

jachin99
Experienced User
Posts: 497
Joined: Sat Feb 13, 2016 8:39 pm

Re: Open a local file using python script?

Post by jachin99 » Wed Oct 11, 2017 3:06 pm

I started out trying f = open("c:\users\\Username\desktop\test.txt") Then I tried f = open("C:\\users\username\desktop\test.txt) and in both cases I get no such file or directory errors. After this, I moved the test file directly to my c drive, adjusted my script and reran it but I got the same errors. From here I tried import OS, and used os.startfile(test.txt) and this opened a text file located directly in my python directory. When I changed os.startfile to open a file on my desktop it threw an error saying the system cannot find the file specified 'c:\test.txt'

Could this just be a permissions issue?

User avatar
kgschlosser
Site Admin
Posts: 4050
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Open a local file using python script?

Post by kgschlosser » Wed Oct 11, 2017 4:17 pm

nope no permissions issue.
you are not telling the open command what you want to do with the file

write to a file

Code: Select all

f = open(r'c:\some_directory\some_file.txt', 'w')
append to a file

Code: Select all

f = open(r'c:\some_directory\some_file.txt', 'a')

read the file

Code: Select all

f = open(r'c:\some_directory\some_file.txt', 'r')
if the file does not exist when using the write or append it will create the file
now good practice if not to use f = as with this way of doing it would require you to call close()

Code: Select all


with open(r'c:\some_directory\some_file.txt', 'a') as f:
    f.write('Sample Text')
If you like the work I have been doing then feel free to Image

jachin99
Experienced User
Posts: 497
Joined: Sat Feb 13, 2016 8:39 pm

Re: Open a local file using python script?

Post by jachin99 » Thu Oct 12, 2017 12:09 am

I think I'm going about this wrong because there are a few different ways to do this, and I could end up learning bits and pieces of each without knowing enough about any one method to accomplish my goal. I want to automate moving .wtv files that I have recorded from my living room to my server. I already have the directory watcher plugin installed in EG where the file will be created but when a new file is created it creates a unique event that gives a specific file name where I want an event to be triggered when a certain file type is created (in my case .wtv). I want my triggered event to be read by either a script or another plugin that moves that file to a network location. I have a network share setup through the sharing wizard in windows seven, and I don't use workgroup, homegroup or any of those other networking protocols. I have found some scripts but when I try them I run into the problem listed in my opening statement. How would I best set this up?

User avatar
kgschlosser
Site Admin
Posts: 4050
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Open a local file using python script?

Post by kgschlosser » Thu Oct 12, 2017 3:40 am

ok you are simply trying to move files without changing them i am gathering.


You are going to need to map the network location as a drive on the computer that is running EG. and we will call that drive letter z:.

what I do not know is if the event has the full path and filename or just the filename. so I am going to assume it's just the filename.

Now the problem i see if that the event is going to take place on file creation that does not mean the file is done being written to. this is going to cause errors if not handled properly. I have to do a bit of research to see if I can locate a way to tell if the file is in use. and if I cannot locate a way I am going to have to run some tests to see if it will only do a copy if the file is not in use. it depends on if the file is locked I would gather.

Now because network transfers of large files (video files are large) can take a long while to transfer depending on the speed of your network and the computer those kinds of things. We do not want to cause a hang in EG. so this is going to have to be done via threading or multiprocessing. I am thinking the latter due to how EG only uses one processor and because the overhead of doing this kind of a thing is rather on the taxing side for system resources. so we want to put some pony's behind it. Give me some time to key it all up.
If you like the work I have been doing then feel free to Image

jachin99
Experienced User
Posts: 497
Joined: Sat Feb 13, 2016 8:39 pm

Re: Open a local file using python script?

Post by jachin99 » Thu Oct 12, 2017 1:11 pm

My recorded TV files don't have to moved immediately so I can solve a few of these problems by scheduling the file move instead. Assuming I do schedule this the only need I would have for directory watcher would be to enable the scheduled task of moving the files but that isn't a neccessity so I can probably cut directory watcher out of the equation. i honestly didn't think of how EG might try to move the file as it is being recorded so I'm glad you brought that up. Just to see how it worked I enabled the directory watcher on my test machine and created a new file. This event log showed this

(DirectoryWatcher.Created(u'C:\\HP\\New TextDocument.txt',)
(DirectoryWatcher.Updated(u'C:\\HP\\New TextDocument.txt',)

I'm not sure what the u means but the rest of that statement I get. I already have the destination folder mapped as a network drive. Maybe this would be even easier if I ran a script to empty a folder at midnight every night. The only thing going into my recorded tv folder is tv recordings, and I should probably build some kind of logic into it that checks to see if any new files have been created. After the files have been moved they can be deleted.

User avatar
kgschlosser
Site Admin
Posts: 4050
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Open a local file using python script?

Post by kgschlosser » Thu Oct 12, 2017 2:07 pm

we are going to K.I.S.S this one. (Keep It Stupid Simple)

well if you only want it to run once a day then use robocopy (Robust File Copy) it is included with Windows. You are able to set up automated copying with it.

robocopy "C:\SOURCE" "C:\DESTINATION" *.wtv /MT /MOV /TS /FP /LOG+:"C:\recording_move.log" /R:3 /RH:2300-0000 /MON:3

/MT = Multi Threaded
/MOV = Moves files (deleting the ones in the source directory)
/R:n = Retry count where n is the number of retries. there is a default wait time of 30 seconds between retries

Logging options
/TS = Include source file time stamps in the output.
/FP = Include full path name of files in the output.
/LOG+:n = Log to a file where n is the file

these next 2 cannot be used at the same time. you will need to pick one or the other
/MON:n = Monitor for changes. where n is the number of changes that have to occur in the source directory before it will start the move
/RH:2300-0000 = copy the files at a specific time. 24 hour clock

I am not sure on the scheduled time deal if it will restart automatically and you only have to run the command once. this you would have to check.

The file monitoring one will always stay running so it will keep a console window open if launched from the command prompt. you can use EG to hide this using the Run Application action.
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 4050
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Open a local file using python script?

Post by kgschlosser » Thu Oct 12, 2017 2:08 pm

and i forgot one thing as well. you do not need to map a network drive when using this. simply put \\computer_name\share\folder in the destination
If you like the work I have been doing then feel free to Image

jachin99
Experienced User
Posts: 497
Joined: Sat Feb 13, 2016 8:39 pm

Re: Open a local file using python script?

Post by jachin99 » Fri Oct 13, 2017 12:38 pm

Thanks for the help. For the time being i'm going to run this script manually because I don't want myself or anyone else to set something to record in the middle of the night, and have this script scheduled to run at the same time. I ran this last night before I went to sleep, and I watched the log update over a the course of about a minute so I will check it when I get time to see if I was successful.

Post Reply