This is my Python file (myfile.py). I am new to Python and programming.
title = "The meaning of life" #myfile.py
And it's present in 'learning_python' directory.
Try this:
from myfile import *
you should then be able to access the variable by it's name. (in your case, title)
Related
Elham Here, I have made a simple code to something like this:
It accepts a string (name) from user
then it do some customization to the string (turn lowercase and replacing space with "_"{under score})
then after customization, it will search for a file with same name as string
then display the content of file. here is its code:
import coder
import time
import calc
mainfunc = ["clears", "action mode"]
main_loop = "main"
coder.clear_screen()
while main_loop == "main":
ask = input("BOT: You say? > ")
mask = ask.replace(" ", "_")
if mask == mainfunc[0]:
print("\n" * 100)
coder.clear_screen()
else:
prt = open(mask, "r")
pr = prt.read()
print(pr)
prt = open(mask, "r")
pr = prt.read()
print(pr)
this is what I can with text, and I have plan to do same thing with some python modules (like instead of displaying its code, it should execute the code), which search for the module name with the string given by user! its like unknown module to be executed with name of string.
I have tried getattr and others like {}.format, ...
but non worked. I have researched the documentation section of functions and modules, but didn't found anything to help or it was too confusing while I don't know name of what I am searching for : (
so if you know the fix, please send the code! You will save my life (I have made a bet with a friend on this :p
-thanks
Python's exec function will execute a string as raw code, which lets you do something like this:
module = input("Enter module to import:")
exec("import " + module)
I'm trying to import another python script with a name of the form fe_fi_fo_fam.py, I'm using importlib.import_module("fe_fi_fo_fam.py") but I'm getting the error : "module fe not found" , how do I get it to read the whole string instead of only the first part? I'm new to python and I seem to have scoured the internet without an answer.
Yo can instead use the built-in function exec after you compose the module string
>>> modOs = 'os'
>>> exec('import ' + modOs)
>>> os.getcwd()
'C:\\Desktop'
Note: Python 3.6 exec
I think you have to understand the difference between module name in Python and file name. importlib.import_module() gets a "module name", not a "file name", so "fe_fi_fo_fam.py" is not a valid argument, instead you have to use "fe_fi_fo_fam" with no .py extension.
In a python project that I collaborate, we're intending initially to parse information from a input fasta file into a dictionary.
Parsing method is already implemented (here and here), and the problem is: code works fine when running in Python3 (fasta file is loaded, its information is parsed for FDB data-strucuture, and then it's saved in a new fdb-file), but when it runs in Python 2, generated dictionary doesn't contains value-information from read fasta file, just the keys.
Links above show code developed for parsing, and block below contains test we execute (which works fine with Python 3 but not save fasta information in Python 2).
print("Instantiating a FastaDB object...")
fasta_db = FastaDB()
print("Defining input file name...")
filename = "../FastaDB/test2.fasta"
username = "inacio_medeiros"
print("Invoking FDB parsing...")
parsed_fdb_structure = fasta_db.ImportFasta(filename, username)
print("Saving in file...")
content = json.dumps(parsed_fdb_structure)
fdb_file_name = filename+".fdb"
fdb_file = open(fdb_file_name, "w")
fdb_file.write(content)
Does anyone have an idea why dictionaries are working fine in Python 3, but not in Python 2?
The problem is not the dictionary, but how classes were created. While on python3 all classes inherit from object class (unless u actually make it inherit from other class), on python2 they dont.
Hence, class A() on python3 is the same as class A(object), but on python2 they are different things: the latter is a "new style class", while the former is an "old style class". I'm a python3 guy too, so this is new for me, but u can find more information on this SO thread
TL;DR: just replace class FDBRegister(): for class FDBRegister(object): and it will work! I tested here ;)
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
Im using the plone cms and am having trouble with a python script. I get a name error "the global name 'open' is not defined". When i put the code in a seperate python script it works fine and the information is being passed to the python script becuase i can print the query. Code is below:
#Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE = request.RESPONSE
# Insert data that was passed from the form
query=request.query
#print query
f = open("blast_query.txt","w")
for i in query:
f.write(i)
return printed
I also have a second question, can i tell python to open a file in in a certain directory for example, If the script is in a certain loaction i.e. home folder, but i want the script to open a file at home/some_directory/some_directory can it be done?
Python Scripts in Plone are restricted and have no access to the filesystem. The open call is thus not available. You'll have to use an External Method or full python module to have full access to the filesystem.