For python files I can get the file name and use to as a prefix for the generated results using:
prefix = os.path.splitext(os.path.basename(main.__file__))[0]
But this fails for ipython notebooks with the following error:
---> 23 return os.path.splitext(os.path.basename(main.__file__))[0]
AttributeError: module '__main__' has no attribute '__file__'
Is there a reasonable way to get the current notebook's name?
Previously suggested solutions, like ipyparams and ipynbname don't seem to work for me.
Someone already posted a workaround using JavaScript.
You can find the original question here: https://stackoverflow.com/a/44589075
Related
I tried to import the dataset on jupyter notebook. But it indicates error as str object is not callable,even the pathway of the file are obsolutely okay.
or Are there any problems with anaconda? help me out!!
here is my code after importing the libraries:
df=pd.read_csv('Nutrients.csv')
Even everything is okay it still shows str object is not callable
Now i need to load the dataset.
In pandas.read_csv, the string which is passed inside as a parameter is the name of the file. If the file does not exist, then python just considers the value as a string which in your case is the same.
Try checking out the location of the jupyter notebook that you are running the code in and the file you want to access. According to your code, they should be in the same location.
I want to find out the filetypes of some files with no file ending.
The best case would be if I could get the same string you get in the file properties. Iam using Python and already tried with mimetypes, which doesn't worked.
Thanks for any help :)
I think your question has been resolved in this issue on stackoverflow :
check type of file without extensions
hopes this help :)
You can try using the file command, executed via subprocess.
result = subprocess.check_output(['file', '/path/to/allcfgconv'])
The resulting string is a bit verbose; you'll have to parse the file type from it yourself.
I am working with nltk in python. I imported the package and downloaded the additional data just fine, but I want to be able to append a new directory to store nltk_data.
When I tried this fix found at this link (How to config nltk data directory from code?)
nltk.data.path.append("path")
I received this error:
AttributeError: 'str' object has no attribute 'append'
What am I doing wrong?
you can try checking what nltk.data.path stores.
most probably you assigned the path as string (may be in a notebook session)
nltk.data.path.append = "folder path" (this is incorrect way)
you should first clear your session or restart your session if you made above change.
Why do I receive the error message in the title from the code below?
Edit: Cause I didn't pay attention to how I wrote "ascii". Thanks everyone
The code below works fine on my Iphone IDE but not on my Windows 7 (w/Notepad++ and Command Prompt). I checked the directory to see if any string.py files existed which I did not see any. I ran a search on my desktop and found 4 files named that, two of which said they were complied. I deleted the compiled files and left the other two. I'm a noob.
import string
import random
x = string.acsii_letters
y = random.choice(x)
print y * 5
It should be string.ascii_letters letters instead of string.acsii_letters. If that's a typo in code statement here only, then your guess must be right, there is another string module in your PYTHONPATH. Open python shell,
import string
print(string.__file__)
to ensure string is being imported from right path. If its not remove that path from PYTHONPATH.
In python 3 I found that using the string.ascii_letters works as string.letters results in an AttributeError.
You have a typo. It should be string.ascii_letters or string.letters. You can look at the attributes of the string module with dir(string) and see what you can access.
i had the same issue the reason was that the name of the file is the same of the module name . so just rename your file the module will work well
I am new to Python, and despite my searching, am unable how to properly access a file from a Python Script on Windows with Python 3. I am trying to use Mongosm to import openstreetmap OSM data into mongodb, but get an error when trying to access the file. How can I fix this? Thank you. According to the github instructions, all I need to do is python insert_osm_data.py <OSM filename> (instructions found here)
The error says:
C:\Users\Jusitn>python C:\Users\Jusitn\Desktop\mongosm-master\insert_osm_data
G:\OSM\planet-140430.osm
File "C:\Users\Jusitn\Desktop\mongosm-master\insert_osm_data.py", line 160
print 'node not found: '+ str(node)
SyntaxError: invalid syntax
insert_osm_data.py is intended to be used with Python 2, but you are apparently running it under Python 3. The easiest fix is to install and use a Python 2 interpreter (compared to rewriting the script for Python 3 compatibility).
I myself is a beginner, but the much I am from the error, it looks like there is a syntax error in you file, as you are using python 3 you should use this : print('node not found: '+ str(node)) in line 160 in you file insert_osm_data.py . In python 3 if you print statement is turned into a print() function.