473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Importing files other than *.py

If I have a Python script in a file named "burp.zz", is there any way
to import it into another script as you can with files named with the
..py extension?

I looked at the imp module, but was not able to make it do what I
wanted. Is this possible, or is the py/pyc extension required?

___/
/
__/
/
____/
Ed Leafe
http://leafe.com/
http://dabodev.com/

Jul 18 '05 #1
2 1389
Ed Leafe wrote in message:
If I have a Python script in a file named "burp.zz", is there any way
to import it into another script as you can with files named with the
.py extension?
I looked at the imp module, but was not able to make it do what I
wanted. Is this possible, or is the py/pyc extension required?
Ed Leafe

Hello Ed,
You can import as usuall if you do this:
Save this to your path.

# zz_import.py
import imputil

# define your custom file handler
def handle_zz(filepath, fileinfo, filename):
sourcefile = open(filepath, 'r')
data = sourcefile.read()
sourcefile.close()
return 0, compile(data, filepath, 'exec'),{}

importer = imputil.ImportManager()
importer.add_suffix('.zz', handle_zz)
importer.install()
# end zz_import.py

Now do this in your code:
import zz_import #now you can import zz files
import burp

If module is not found it will raise an ImportError.

HTH,
M.E.Famer
Jul 18 '05 #2
Ed Leafe wrote in message:
If I have a Python script in a file named "burp.zz", is there any way
to import it into another script as you can with files named with the
.py extension?
I looked at the imp module, but was not able to make it do what I
wanted. Is this possible, or is the py/pyc extension required?
Ed Leafe

Hello Ed,
You can import as usuall if you do this:
Save this to your path.

# zz_import.py
import imputil

# define your custom file handler
def handle_zz(filepath, fileinfo, filename):
sourcefile = open(filepath, 'r')
data = sourcefile.read()
sourcefile.close()
return 0, compile(data, filepath, 'exec'),{}

importer = imputil.ImportManager()
importer.add_suffix('.zz', handle_zz)
importer.install()
# end zz_import.py

Now do this in your code:
import zz_import #now you can import zz files
import burp

If module is not found it will raise an ImportError.

HTH,
M.E.Farmer
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jeff Wagner | last post by:
I am importing a file which contains a persons name (firstName, middleName, etc). If I define a function to do this, how can I use the variables outside of that function? Here is the code: ...
2
by: Awah Teh | last post by:
I am working on a project that involves importing IIS Log files into a SQL Server database (and these logfiles are big --> Some up to 2GB in size). Up until now I thought that DTS or the BULK...
2
by: steve | last post by:
Hi, I have researched but have not found a good solution to this problem. I am importing large amounts of data (over 50 Meg) into a new mysql db that I set up. I use >mysql dbname <...
1
by: Steve George | last post by:
Hi, I have a scenario where I have a master schema that defines a number of complex and simple types. I then have a number of other schemas (with different namespaces) where I would like to reuse...
11
by: Grim Reaper | last post by:
I am importing a .csv file into Access that has 37 fields. My problem is that sometimes the last field only has data at the end of the column (it looks like when you import a file into Access, for...
1
by: D Mat | last post by:
Hi, I'm trying to get MS Access 2000 to automatically import a series of (~200) flat text, tab delimited, data files into a single Access table, with consistent fields and rows. The files have...
1
by: sparks | last post by:
I have never done this and wanted to ask people who have what is the best way. One person said import it to excel, then import it into access table. but since this will be done a lot, I am...
5
by: dixie | last post by:
If I sent a user an empty database container - dB with no tables and I needed them to import their tables into it and one of their tables was a hidden table with the prefix Usys, is there any way...
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
1
by: Kosmos | last post by:
Hey guys, I'm fairly familiar with access by now, but I've never worked with importing text into an access database. I have some fairly large text files (lets say, for example, a folder of 20 text...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.