473,808 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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\Answe rs.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 1661
dwblas
626 Recognized Expert Contributor
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.tx t", and store them all in the same directory.
May 20 '10 #2
Motoma
3,237 Recognized Expert Specialist
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.pat h[0], 'Answers.txt')
May 21 '10 #3
Glenton
391 Recognized Expert Contributor
If it finds the file and doesn't write to it, then the problem is not the path! open("answers.t xt","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.t xt").
May 21 '10 #4

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

Similar topics

1
1646
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 fine and a file can be saved to the hard drive. but deserialization blows chunks, because the exe is looking in its own folder, or one of its sub folders, for the dll, and the dll is in a totally seperate folder.
2
1600
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. I'm using this for FileShares: DirectoryInfo di = new DirectoryInfo(strURL); FileInfo fileList = di.GetFiles(strPattern); and then I compare the LastWriteTime of all the files in the array.
9
8338
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) and two folders (Subfolder 1, Subfolder 2). .......I need to delete File1, File2, File3. Subfolder 1 contains FileA. .......Need to delete FileA. Subfolder 2 contains FileB, FileC, FileD.
3
1597
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 want to navigate to in the bin folder along with the exe. I then put the following line in the load of my application: Me.webControl.Navigate(".\summarypage.html")
1
1580
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, Rachit
1
1364
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 still doesn't seem to be finding it. Am I doing something wrong? try { in = new Scanner(new File("File Reading.txt")); String test = in.nextLine();
3
1557
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: C:\Python25\Lib\site-packages\spam\). How do i do that?
0
1108
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 direction how to do it in VB.net? Thank you very much! CLC
1
3914
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 please? What I am looking for is to invoke a StreamReader (or other suitable reader) and read in the contents of this folder, so to populate string values in the code. An example of the code would look something like:
1
3496
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 then .class file is redirected to another folder. thanks........
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10631
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.