473,385 Members | 1,320 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.

No such file or directory:

Ok i think it is the way i am telling the function what the path is. im guessing this only doesnt work because im on a windows machine (on python 2.5)

Expand|Select|Wrap|Line Numbers
  1. f = open('files\test.txt', 'r')
  2. for line in f:
  3.     print line
ive tried changing the string in all obvious ways

Expand|Select|Wrap|Line Numbers
  1. 'files\\test.txt'
  2. r'files\test.txt'
  3. 'files/test.txt'
  4. 'c:\....\files\test.py'
  5. 'c:\\...\files\test.py' (for the c:\ bug i tried c:\\)
  6.  
Ok what am i missing here? ive been coding for years and this has stumped me. Google doesnt even have an answer. do people just not open files out of there directories in windows?
Jan 19 '10 #1
4 11141
bvdet
2,851 Expert Mod 2GB
Look what happens when a string with a backslash character is evaluated:
Expand|Select|Wrap|Line Numbers
  1. >>> print 'abcdef\thrt'
  2. abcdef    hrt
  3. >>> print r'abcdef\thrt'
  4. abcdef\thrt
  5. >>> 
Characters "\t" are evaluated as a tab character. Escape the backslash characters for a path as in:
'D:\\SD\\plugins\\Embed\\Defaults\\EmbedPL.txt'

or use the forward slash as in:
'D:/SD/plugins/Embed/Defaults/EmbedPL.txt'

or use a raw string as in:
r'D:\SD\plugins\Embed\Defaults\EmbedPL.txt'

If the file is in your current working directory (os.getcwd()), you can omit the full path as in:
'EmbedPL.txt'

To read a file in the directory just above the CWD:
open("..\\EmbedPL.txt").read()

To read a file in a directory just below the CWD:
open("subdir_name\\EmbedPL.txt").read()
Jan 20 '10 #2
thanks for the reply, turns out i miss typed the file when making it. to text.txt. but i got it working now thanks, one question though. if the file is dynamic eg
filename from sys.
Expand|Select|Wrap|Line Numbers
  1. f=open('files/'.filename,'r')
  2.  
will this work for any value of filename, where filename is just a file (not a path) and the file exists.
Jan 20 '10 #3
bvdet
2,851 Expert Mod 2GB
iamfletch:

Look at the glob module. Usage:
Expand|Select|Wrap|Line Numbers
  1. glob.glob(pathname) or glob.glob1(dirname, pattern)
From help(glob):
Returns a list of paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la fnmatch.

You may be referring to a file name stored in a variable. You can use string formatting. Example:
Expand|Select|Wrap|Line Numbers
  1. var = "filenameXYX.txt"
  2. open("dir1/dir2/%s" % (var))
Jan 20 '10 #4
Yup, that's all clear now. Thanks a lot, fast and helpful forums here.
Jan 20 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
5
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up...
2
by: Jack David | last post by:
Using the code below I am able to monitor a single directory for a new file and then kick-off a process to deal with the file. The question is??? How would I modify this code to be able to monitor...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
4
by: Matt Jensen | last post by:
Howdy I've got a rather strange issue occuring. I used forms based .NET authentication, although I'm also setting some session variables when people login. However, I've found when people use...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
by: tasahmed | last post by:
Hello Friends, I wrote a function which scans the current working directory and lists out details such as directory/file owner, permission etc. The output of this script can be viewing in the...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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.