[Tutor] inserting path to open a file from a variable

richard kappler richkappler at gmail.com
Thu May 28 20:40:37 CEST 2015


Sorry Alan, I think I might have accidentally replied to only you. Love
your Python Projects book btw! Working through it now.

I've looked at, and played with a bit, ConfigParser and yes, it seems that
may be a better idea, thank you.

I still have the issue of how to get the path into my code, regardless of
whether I use ConfigParser or

What I've tried (none worked):
> file = open(rd1, 'r')
>

This should have worked.

What happened when you tried it? "Did not work" is a tad vague!

Traceback (most recent call last):
  File "21FileMonitor.py", line 28, in <module>
    file = open(rd1, 'r')
IOError: [Errno 2] No such file or directory:
'Documents/MyScripts/fileMonitor/log.txt'

Should I not be using os.path or somesuch? If my path to the file, as
provided via ConfigParser, is rd1, would I do:

file = os.path.open(%s, 'r') % (rd1)
or
file = os.path.open(rd1, 'r')

or do I not need os.path...

Kind of lost in the woods here.

regards, Richard

On Thu, May 28, 2015 at 1:56 PM, Alan Gauld <alan.gauld at btinternet.com>
wrote:

> On 28/05/15 18:39, richard kappler wrote:
>
>  I've created a config file which the user would edit named
>> fileMonitor.conf:
>>
>> # line two is the absolute path to the log you are parsing data from
>> # keep 'rdfile:' as is, path starts after it, no spaces
>> rdfile:Documents/MyScripts/fileMonitor/log.txt
>> # line 4 is the absolute path to the log you are appending the parsed data
>> too
>> # keep 'wrtfile:' as is, path starts after it, no spaces
>> wrtfile:Documents/MyScripts/fileMonitor/newlog.txt
>>
>
> You should maybe look at the config parser module and
> use a standard file format. You are likely to be adding
> more entries into this file...
>
>  and I have written up a script that reads the paths and strips off the
>> unusable bit named readConfig.py:
>>
>> # read the config file to get file locations for a script
>> conf = open('fileMonitor.conf', 'r')
>> read_it = conf.read()
>>
>> for line in read_it.splitlines():
>>
>
> You should consider using something like
>
> with open('fileMonitor.conf', 'r') as conf:
>    for file in conf:
>
> Rather rthan reading/splitting the file in memory.
>
>  rd1 = rd.replace('rdfile:',"",1)
>> wrt1 = wrt.replace('wrtfile:',"",1)
>>
>> This worked fine, if I print rd1 and wrt1 I get just the paths that are
>> entered into the conf file.
>>
>> file = open('log.txt', 'r')
>>
>> but I need to replace 'log.txt' with rd1 (path and file name to log.txt).
>>
>> What I've tried (none worked):
>> file = open(rd1, 'r')
>>
>
> This should have worked.
>
> What happened when you tried it? "Did not work" is a tad vague!
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 

Windows assumes you are an idiot…Linux demands proof.


More information about the Tutor mailing list