How to get self file name [duplicate] - python

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
python, path of script
Is there any way that i can print the self filename of a file and current location.

Could __file__ be what you are looking for?

Related

How can I find full path for a file? [duplicate]

This question already has answers here:
How to get an absolute file path in Python
(11 answers)
Closed 2 years ago.
Let's say I have this path
D:\something\something1\from_here_I_now\stuff\stuff2.
So, I know that \from_here_I_now\stuff\stuff2 is a permanent path, but the beginning is different, like I know that D:\something\something1\ may be different for someone else. How can I find the D:\something\something1\ knowing only \from_here_I_now\stuff\stuff2?
Try something like this:
import os
filestr = '\from_here_I_now\stuff\stuff2'
fullstr = os.path.abspath(filestr)
print(fullstr)
>>> 'D:\something\something1\from_here_I_now\stuff\stuff2'
print(fullstr[:len(filestr)])
>>> 'D:\something\something1'

python sort file path based on created time [duplicate]

This question already has answers here:
How do I get file creation and modification date/times?
(12 answers)
How do you get a directory listing sorted by creation date in python?
(19 answers)
Closed 2 years ago.
I have python array with filepath in it, I need to sort the files path based on created date. Is there any inbuilt functionality exist for it.
files = ["/path1/filename.text", "/path2/filename.text", "/path2/filename.text"]

Search a CSV-file with python [duplicate]

This question already has answers here:
How do I check whether a file exists without exceptions?
(40 answers)
Closed 4 years ago.
I want to search a CSV-File with python2 on the Raspberrry Pi. If the file is not found the program should generate it. How I can search a file and can decide with an if-statment if there is CSV-file or not?
as #John Anderson said you can use the os module's os.path.
if os.path.exists('path/to/your/csvfile/') is True:
print("Do something")

Manipulate variables in Python [duplicate]

This question already has answers here:
How to get only the last part of a path in Python?
(10 answers)
Closed 5 years ago.
Is it possible to manipulate a variable, for example:
file = "/Python/work.txt"
to just list work.txt, without /Python? excluding everything on the left of the "/"?
Thank you
Of course! Simply do this:
file = "/Python/work.txt"
excluded = file.split("/")[-1]
This would return "work.txt" in the excluded variable.

How do I get the modified date/time of a file in Python? [duplicate]

This question already has answers here:
How do I get file creation and modification date/times?
(12 answers)
Closed 9 years ago.
How do I get the modified date/time of a file in Python?
os.path.getmtime(filepath)
or
os.stat(filepath).st_mtime
Formated:
import time
print time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(fname)))

Categories