473,396 Members | 2,089 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,396 software developers and data experts.

How to find a file within the same folder of a .py

1
I've made a simple script which asks questions, and writes the answers into a file (Answers.txt) However, the only way I get it to work is to define the entire path, e.g. C:\folder\Answers.txt

But Answers.txt can be in different folders depending on where users have installed it, so I'm wondering if I can make the script search within its own folder? That way people would be able to simply place the two in the same folder and not have to rewrite the pathing.

I tried simply writing into \Answers.txt but it didn't work. It seems to find the file, but doesn't write to it.
May 20 '10 #1
3 1648
dwblas
626 Expert 512MB
You want to always use the complete path. It will avoid a lot of needless debugging over the years. The simplest solution if I understand, would be to append the person's name to the file name, "Answers_Bob.txt", and store them all in the same directory.
May 20 '10 #2
Motoma
3,237 Expert 2GB
To write to a file in the current working directory (the directory the user is currently in) you would just write to 'Answers.txt'.

To write to the directory that the script is in, you would write to os.join(sys.path[0], 'Answers.txt')
May 21 '10 #3
Glenton
391 Expert 256MB
If it finds the file and doesn't write to it, then the problem is not the path! open("answers.txt","w") is what you need if you want to over-write the file each time. Or "a" to append. Or "r" to read-only (this is the default), which is why I suspect you were using open("answers.txt").
May 21 '10 #4

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

Similar topics

1
by: john | last post by:
I'm having a serialization problem. I have a dll, and one of its classes can get serialized. but the dll and the calling exe dont reside in the same folder or subfolder. so serialization works...
2
by: Rosa | last post by:
Hello Everybody, Is there a class equivalent to DirectoryInfo and FileInfo where I can pass a http:// URL instead of a network path in order to find the youngest file within an Intranet Folder. ...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
3
by: Carl Gilbert | last post by:
Hi I have a Windows WebControl on my form and I plan to burn the application to a CD. The applcation has to navigate to a page that I am keeping in the root of the CD. I have put the page I...
1
by: rachit.goyal | last post by:
Hi All, How can I retrieve list of all subfolder within one folder? I want retrieve this list using path of input folder. I want to achieve this in windows form application using VC++. Regards,...
1
by: Empyrean | last post by:
I'm attempting to make my first program that involves file input, but I'm running into problems finding the .txt file. I placed the file inside the same folder as the rest of the project, but it...
3
by: Sergio Correia | last post by:
I have a program in 'C:\Python25\Lib\site-packages\spam\spam.py' Importing and everything works fine: But the program calls a file located on the same folder (that is:...
0
by: =?Utf-8?B?Q0xD?= | last post by:
Hi, My application requested to merge multiple existing pdf files in the same folder to one PDF file using VS.net 2005 I am pretty new in Vb.net programming, could anyone please give me some...
1
by: E11esar | last post by:
Hi there. I have a folder within my asp.net (c#) project in VS2008 called Files. Within this folder is a text file called files.txt. Is there a way to access this folder from the C# code...
1
anurag275125
by: anurag275125 | last post by:
Hello all, When we compile java program it creates .class file which is created in the same folder in which the source program is. Is there any mechanism by which, when we compile the source code...
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:
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.