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

how to get file name of the running .py file

Hi,

How to get the name of the running .py file like the macro _FILE_ in C?

Thanks.
Jason

Aug 23 '06 #1
5 2345
Jason Jiang wrote:
Hi,

How to get the name of the running .py file like the macro _FILE_ in C?

Thanks.
Jason
import os
import sys
print sys.argv[0]

or if you just want the script and not the full path

print os.path.basename(sys.argv[0])

-Larry Bates
Aug 23 '06 #2
Larry Bates wrote:
Jason Jiang wrote:
Hi,

How to get the name of the running .py file like the macro _FILE_ in C?

Thanks.
Jason

import os
import sys
print sys.argv[0]

or if you just want the script and not the full path

print os.path.basename(sys.argv[0])

-Larry Bates
Also, check out:
http://groups.google.ca/group/comp.l...2572b3c2f2cb13

Peace,
~Simon

Aug 23 '06 #3
Thank you guys.

Jason

"Simon Forman" <ro*********@yahoo.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Larry Bates wrote:
>Jason Jiang wrote:
Hi,

How to get the name of the running .py file like the macro _FILE_ in C?

Thanks.
Jason
import os
import sys
print sys.argv[0]

or if you just want the script and not the full path

print os.path.basename(sys.argv[0])

-Larry Bates

Also, check out:
http://groups.google.ca/group/comp.l...2572b3c2f2cb13

Peace,
~Simon

--
http://mail.python.org/mailman/listinfo/python-list


Aug 24 '06 #4

Jason Jiang wrote:
Hi,

How to get the name of the running .py file like the macro _FILE_ in C?
There are many ways--IMO the easiest is with __file__:
>>print __file__
/home/bill/.pystart
>>>
[tmp]$ cat foo.py
#!/usr/bin/env python

print "The name of the file is:%s"%__file__
[tmp]$ ./foo.py
The name of the file is:./foo.py

Aug 24 '06 #5
Larry Bates wrote:
>How to get the name of the running .py file like the macro _FILE_ in C?
import os
import sys
print sys.argv[0]

or if you just want the script and not the full path

print os.path.basename(sys.argv[0])
except that __FILE__ is the name of the current source file, not the
name of the main program.

the global name __file__ is a better choice; when definied, it points to
the file from which the current module was loaded (this might be a byte
code file or a shared library).

also see:

http://pyref.infogami.com/__file__

</F>

Aug 24 '06 #6

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

Similar topics

6
by: Serge calderara | last post by:
Dear all, Does any one have a ready class that is able to access a configuration file and make different type of querry, read, write operation? Why re invanting the real if something already...
6
by: moonriver | last post by:
I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear: 13/07/2004...
5
by: Tim Eliot | last post by:
Just wondering if anyone has hit the following issue and how you might have sorted it out. I am using the command: DoCmd.TransferText acExportMerge, , stDataSource, stFileName, True after...
4
by: Brian K. | last post by:
I am trying to split up and secure a database that we've been using on a network for a few years, following the procedure listed in Q304932 from microsoft. I create a new blank database for...
4
by: Lance | last post by:
Are there any methods that indicate whether a string can be used as a full path for a file? For example, what I'm looking for is a method that would test for things like correct file name format,...
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"...
0
by: JohnLucas | last post by:
Hi all, I have just started working with the XML::Simple module to parse an XML file. I'm trying to pull some values from the file that I need in another program. The problem is that the XML...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
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?
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:
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
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...
0
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...
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,...
0
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...

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.