473,382 Members | 1,353 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,382 software developers and data experts.

Files, directories and imports - relative to the current directoryonly

ptn
Hello, group.

I can only read files and import modules that are in the same
directory
as the one my script is. Here is a test script (path.py):

import os
import uno # some module I wrote

print list(os.walk('~/hacking/python'))
f = open('~/read/foo.txt')
print f.read()

And here is the output:

Traceback (most recent call last):
File "path.py", line 2, in <module>
import uno
ImportError: No module named uno

If I comment that import, the output becomes this:

[]
Traceback (most recent call last):
File "path.py", line 4, in <module>
f = open('~/read/foo.txt')
IOError: [Errno 2] No such file or directory: '~/read/foo.txt'

(Notice the empty list at the beginning, that would be the output of
os.walk().)

I have added this line to my .bashrc:
export PYTHONPATH=$PYTHONPATH:~/hacking/python
I thought that by doing so all the scripts found in that directory and
all it's children would be available for import, but that doesn't seem
to be the case. As for the other problems, I have no idea.

So, what's wrong here? Maybe there's something I haven't set up?
Mar 25 '08 #1
2 1719
ptn wrote:
Traceback (most recent call last):
File "path.py", line 4, in <module>
f = open('~/read/foo.txt')
IOError: [Errno 2] No such file or
directory: '~/read/foo.txt'
[...]
So, what's wrong here? Maybe there's something I haven't set up?
Simple: the directory "~" doesn't exist. Since you're not using a
shell (but direct file access) there is no tilde expansion, and "~"
is treated as a regular file name. If you need to get the home
directory refer to the environment variable HOME
(os.environ["HOME"]). There even may be a shorter way, please refer
to the os module docs.

Regards,
Björn

--
BOFH excuse #139:

UBNC (user brain not connected)

Mar 25 '08 #2
En Tue, 25 Mar 2008 15:35:34 -0300, Bjoern Schliessmann
<us**************************@spamgourmet.comescri bió:
ptn wrote:
> Traceback (most recent call last):
File "path.py", line 4, in <module>
f = open('~/read/foo.txt')
IOError: [Errno 2] No such file or
directory: '~/read/foo.txt'
[...]
So, what's wrong here? Maybe there's something I haven't set up?

Simple: the directory "~" doesn't exist. Since you're not using a
shell (but direct file access) there is no tilde expansion, and "~"
is treated as a regular file name. If you need to get the home
directory refer to the environment variable HOME
(os.environ["HOME"]). There even may be a shorter way, please refer
to the os module docs.
That shorter way being os.path.expanduser
http://docs.python.org/lib/module-os.path.html

--
Gabriel Genellina

Mar 25 '08 #3

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

Similar topics

0
by: Rutger Claes | last post by:
In the directory /var/www/dynamo/xsl is the basic xsl file for xhtml documents /var/www/dynamo/xsl/xhtml.xsl. In this file there is an import statement: <xsl:import href="xhtml/basic.xhtml.xsl"...
1
by: Xif | last post by:
Hello Everyone! Here's a problem with relative imports: Suppose I have a package called some_package (in a separate directory included in the PYTHONPATH, with an __init__.py file etc.) This...
6
by: Christopher | last post by:
What is the syntax for including files from another directory? In my case I want to include a file called "error.h" which lies in a directory called "support" which is a directory inside the...
10
by: Martin Ho | last post by:
I am running into one really big problem. I wrote a script in vb.net to make a copy of folders and subfolder to another destination: - in 'from.txt' I specify which folders to copy - in...
23
by: **Developer** | last post by:
Is there an easy way to copies all files in a directory into another directory? What about coping subdirectories too? Thanks in advance for any info
2
by: s99999999s2003 | last post by:
hi I am working in unix and i have some directories names with spaces eg ABC DEF A how can i work effectively with spaces in directory/file names in python? sometimes when i do...
8
by: Anil Gupte | last post by:
Hi all! Hope you can help me. I want to find all files matching a wildcard in all subdirectories. Is there a quick way to do that? For example in DOS I can go dir *.txt /s and it will find...
0
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.