I am currently trying to code a searching program, which makes use of a program I've already written. It refuses to get to the second print statement.
print("Relevance: ")
# import sqlite3
import Breakdown.py as bd
import re, nltk
from nltk.corpus import wordnet
# from sqlite3 import Error
from autocorrect import spell
print("Input line: ")
The file structure looks like this:
However, I can't work out why it can't get past that import section.
This is somewhat important.
Thanks.
Just write:
import Breakdown as bd
python will import the Breakdown.py file as a module. It will be looking for any variable or function named "py" in the Breakdown module if you use:
import Breakdown.py as bd
... which I don't think is the case here.
You should put the Breakdown.py file in the path where you're starting Python or in one of the directories where Python looks for libraries:
import os
for p in sys.path:
print(p)
and use import Breakdown (no .py).
Or else add to sys.paththe folder where the module is with:
sys.path.append('/your/foldername')
Related
I'd like to know how to import a dataframe based on part of the file name
I have a file like: 'Report_Lineup_Export_20220809_1354.xls' where the numbers are de date and hour when you donwloaded the file.
I'm importing it like:
lineup_source = pd.read_excel('C:/Users/fernandom/OneDrive/08_Scripts/01_Python/Report_Lineup_Export_20220809_1354.xls')
I want my code to read whatever is in the folder that starts with 'Report_Lineup_Export', ignoring the last bit.
Thanks.
You can use glob module:
from glob import glob
import os
for filename in glob(os.path.join(_mydir, 'Report_Lineup_Export*.xlsx')):
do_something_with_filename(filename)
Note that you can cut out .xlsx if you'd like to get all kind of files starting with that initial string.
Adding to the above answer, you can do it with the pathlib module in Python.
from pathlib import Path
for file in Path('some/dir').glob('*.xlsx'):
do_something(file)
Is there any module named regex to use like : regex.compile(pattern, string)
i tried to import it like this: import re, regex and I found this error ModuleNotFoundError: No module named 'regex'
you need to import the python regex library :
https://docs.python.org/3/library/re.html
by doing: import re and then you can put any name you want like regex
such as import re as regex
import re
pattern=re.compile('apple')
result=pattern.findall('apple Dipendra apple')
print(result)
You cannot import packages the way you are importing. I guess your work can be done with the python inbuilt "re" package. Please take a look into the above code snippet by me for your reference
I've succeeding in importing a single file, but the file calls other files and I get an error. So I'm trying to import and entire folder. I would prefer not to import each file one by one since I know it must be possible to import the whole folder. Here is the syntax I used to import a file:
import importlib.machinery
import os
temp_directory2 = '/Users/me/PycharmProjects/inference_engine2/inference2/ancient/temp.py'
temp_directory = '/Users/me/PycharmProjects/inference_engine2/inference2/Proofs/main_loop.py'
main_directory = '/Users/me/PycharmProjects/inference_engine2/inference2/Proofs/'
b = os.path.exists(temp_directory)
loader = importlib.machinery.SourceFileLoader('temp', temp_directory)
handle = loader.load_module('temp')
You can add the path to the list sys.path at the beginning of your file, like so:
import sys; sys.path.insert(0, r'C:/Users/me/PycharmProjects/inference_engine2/inference2/Proofs')
Note since you are inserting the path at the beginning of the list, this is the first place python will go to look for a module.
Convert it to a package using __init__.py. more here : https://docs.python.org/2/tutorial/modules.html
Im using os.startfile to start a bat but i can't find a way to start a bat file that has a period infront of its name.
example of what i want to achieve:
import os
import subprocess
os.startfile('C:\\folder\\proov\\.batch.bat')
Use raw strings:
import os
os.startfile(r'C:\folder\proov\.batch.bat')
Note that for a batch file, you can simply do:
import os
os.system(r'C:\folder\proov\.batch.bat')
Or
import subprocess
subprocess.call(r'C:\folder\proov\.batch.bat')
import os
os.startfile(os.path.join("c:\\folder\\proov\\", ".batch.bat"))
My teacher put up a large data in the school server and gave me a piece of code to open up the files. Below is my code:
import sys
sys.path.append("/Data/Data123/.local/lib/python2.7/site-packages")
from cloud.common import get_tile
from scipy.io import netcdf_file as copen
from scipy import interpolate
import matplotlib
from matplotlib import pyplot as pp
import matplotlib.cm as cm
import numpy
path = '/Data/a'
filenames = ['Hello.nc']
But when I run the code, it says there is "No such file or directory :'Hello.nc'
I am sure files are in that directory. So I want to ask, what did I do wrong?
Or am I not even collecting to the directory?
Thanks
It would be very difficult to answer this question without knowing what files are on the server and also looking at the code that actually connects, and attempts to retrieve the file; but I can provide you with this suggestion.
Use os.path instead of declaring a literal path string. For example:
Change:
path = '/Data/a'
To:
import os
path = os.path.join(r'/Data', 'a')
Then, when you concatenate path with a filename, use os.path.join again:
os.path.join(path, filenames[0]) # Modify to fit your filenames loop accordingly
The problem may be in the way you combine path with a filename. If this does not help, please add more code or double check the server to make sure the file is there.