我正在用Python编写代码,以在特定目录(包括其子目录)中列出所有文件及其大小和创建日期。我最终得到的代码仅适用于当前目录,而不适用于特定目录。 如果我将变量文件夹的值替换为特定目录,则会出现错误。
下面是我的代码:
class myAnnotation: MKPointAnnotation {
var objectId:String = ""
var myImage:String = ""
}
//annotation for the points
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is myAnnotation else { return nil }
let myAnnotation = annotation as! myAnnotation
let identifier = "annotationView"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
print (myAnnotation.myImage)
if annotationView == nil {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}
//Pin with Title
if annotation.title == "fire" {
annotationView?.image = UIImage(named: "fire")
} else if annotation.title == "water"{
annotationView?.image = UIImage(named: "water")
}
return annotationView
}
func addStickForDanger(_ title:String, report:Reports){
let droPin = myAnnotation()
if (report.type!).boolValue {
droPin.title = "fire"
} else {
droPin.title = "water"
}
droPin.coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(truncating: report.lat!), CLLocationDegrees(truncating: report.lon!))
droPin.objectId = report.objectId!
myMap.addAnnotation(droPin)
}
答案 0 :(得分:0)
对于特定目录,您需要将根路径与文件路径连接起来以获得完整路径。
for root, dirs, files in os.walk(folder):
for list in files:
list=os.path.join(root,list) # joining root and the file name for full path
file_size = os.path.getsize(list)
createDate = time.ctime(os.path.getctime(list))
listOfFiles = list, "Size: %.1f bytes"%file_size, "Created date: " + createDate
print(listOfFiles)