python sort file path based on created time [duplicate] - python

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"]

Related

How to use the concept of indexing dictionary in python [duplicate]

This question already has answers here:
Append a dictionary to a dictionary [duplicate]
(7 answers)
How to index into a dictionary?
(11 answers)
Closed 2 months ago.
I encountered a program where I had to append one dictionary into another, and I was unable to do it because I could not use indexing in dictionary so I wanted to know what the possible ways are I could do it

looping through elements and using them as names for objects in python [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
eval(): can't assign to function call
(1 answer)
Closed 2 years ago.
for N_RECORDS in ['50k','50m','100m','200m','python']:
eval("res_"+N_RECORDS) = spark.read.load("/tmp/xgb-rts/xgb_results_"+N_RECORDS+"_pos.csv")
eval("res_"+N_RECORDS+"_neg")=spark.read.load("/tmp/xgb-rts/xgb_results_"+N_RECORDS+"_neg.csv")
I just want to create objects with the names res_50k, res_50m, res_100m, res_50k_neg, etc without doing it manually.
However, I keep getting an error
SyntaxError: can't assign to function call
File "<command-1997330779535506>", line 2
eval("res_"+N_RECORDS) = spark.read.load("/tmp/xgb-rts/xgb_results_"+N_RECORDS+"_pos.csv"
How do I work around this?

How can I split string to several folders? [duplicate]

This question already has answers here:
How to split a dos path into its components in Python
(23 answers)
Closed 4 years ago.
I have a case like this:
Path = 'C:\Intel\ExtremeGraphics\CUI\Resource'
I want to split this string to list with the string folders names:
['C:', 'Intel', 'ExtremeGraphics', 'CUI, 'Resource']
How can I do that in the shortest way?
folders = Path.split('\')
It will return you the required list of the folders

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