Hi how do I import files inside a txt file?

Joel Goldstick joel.goldstick at gmail.com
Mon Sep 2 09:28:40 EDT 2019


On Mon, Sep 2, 2019 at 9:21 AM Spencer Du <spencerdu at hotmail.co.uk> wrote:
>
> On Monday, 2 September 2019 15:03:52 UTC+2, Joel Goldstick  wrote:
> > On Mon, Sep 2, 2019 at 8:46 AM Spencer Du <spencerdu at hotmail.co.uk> wrote:
> > >
> > > On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid  wrote:
> > > > Spencer Du <spencerdu at hotmail.co.uk> writes:
> > > >
> > > > > How do i import files inside a txt file if they exist in the current directory?
> > > > >
> > > > > Here is the current code but I dont know how to do it correctly.
> > > > >
> > > > > import paho.mqtt.client as mqtt
> > > > > from mqtt import *
> > > > > import importlib
> > > > > import os
> > > > > import os.path
> > > > > # from stateMachine import *
> > > > >
> > > > > with open("list_of_devices.txt", "r") as reader:
> > > > >     for item in reader:
> > > > >             try:
> > > > >                     os.getcwd()
> > > > >                     print("hi")
> > > > >             except:
> > > > >                     print("error")
> > > > >
> > > > > This is "list_of_devices.txt":
> > > > > test1,test2
> > > > >
> > > > > Each name refers to a python file.
> > > > >
> > > > My interpretation is that you want to read a file (list_of_devices.txt)
> > > > and this file contains names of other files and you want to read those
> > > > files as well and do something with them (read or print or whatever).
> > > >
> > > > You can approach it like this: write a function to read a file and work
> > > > on it. Like this,
> > > >
> > > > def fn(fname):
> > > >     with open(fname, "r") as f:
> > > >          try:
> > > >                 # work with f
> > > >          except:
> > > >                 print("error")
> > > >
> > > > Then use this function in your code that you have writen. Like this
> > > >
> > > > with open("list_of_devices.txt", "r") as reader:
> > > >      for item in reader:
> > > >          try:
> > > >                 fn(item)
> > > >          except:
> > > >                 print("error")
> > > >
> > > > In the example that you gave, you have written contents of
> > > > "list_of_devices.txt" as
> > > >
> > > > test1,test2
> > > >
> > > > Take care to read them as comma separated. Or if you have control then
> > > > write them on separate lines.
> > > >
> > > > Regards.
> > > > --
> > > > Pankaj Jangid
> > >
> > > Hi Pankaj
> > >
> > > I dont understand so what is complete code then?
> > >
> > > Thanks
> > > Spencer
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> >
> > Pardon me for guessing, but your question seems to imply that you know
> > how you want to do something .. but I'm not sure you have tackled your
> > problem correctly.
> >
> > My guess is:  Depending upon the names listed in a text file, you want
> > to do different imports into your program.   You don't yet know how to
> > read a file with python.
> >
> > First, when you run your program, python compiles it in order.  Since
> > you don't know what you want to import until after you run your
> > program, you can't import those modules.  You may be able to run a
> > program to read the module list, then have it output to a new file the
> > code you eventually want to run based on the modules you discovered.
> > That sounds cute in a way, but probably not in a good way.  You could
> > also surround import statements with try/except code that will import
> > what it can, and alert you when it can't
> >
> > Can you give us the bigger picture of what you want to accomplish?
> > This might lead to a better solution than the one you are thinking of
> > now
> >
> > --
> > Joel Goldstick
> > http://joelgoldstick.com/blog
> > http://cc-baseballstats.info/stats/birthdays
>
> Hi
>
> I have a txt file which contains the names of files. They are .py files. I want to import them into a python file if they exists in current directory and if the name of file does not exist then print out error and not import. How do I do this?
>
> Thanks
> Spencer

Here is a discussion on Stack overflow that lays out how you can
dynamically import files.  This should get you started in the right
direction.  First, see if you can write code to read the file, and
retrieve the names of the modules you want to import.   Come back if
you stumble with your code for that

https://stackoverflow.com/questions/301134/how-to-import-a-module-given-its-name-as-string
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays



More information about the Python-list mailing list