Created
October 19, 2021 11:32
-
-
Save Shrestha7/237d80f81404335d28d693188b871f17 to your computer and use it in GitHub Desktop.
file handling in models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from typing import Reversible | |
from django.db import models | |
from os import listdir, path | |
from os.path import isfile, join | |
import os | |
import glob | |
# Create your models here. | |
class insp(models.Model): | |
xml =models.FileField(upload_to='xml/') | |
# def post_save(self,*args,**kargs): | |
list_of_files = glob.glob(str('E:/Projects/demodj/alpha/templates/xml/*')) | |
print(list_of_files) | |
latest_file = max(list_of_files, key=os.path.getctime) | |
print(latest_file) | |
with open(latest_file) as file: | |
file.readlines(3) | |
file.append('<tei>') | |
# for onlyfiles in glob.iglob(str('E:/Projects/demodj/alpha/templates/xml/*'),recursive=True): | |
# print(onlyfiles) | |
# onlyfiles = glob.glob(str('E:/Projects/demodj/alpha/templates/xml/')) | |
# print(onlyfiles) | |
# for files in onlyfiles: | |
# print(files) | |
# onlyfiles = 'E:/Projects/demodj/alpha/templates/xml/'.glob('*') | |
# onlyfiles = [f for f in listdir(str('E:/Projects/demodj/alpha/templates/xml/'))] | |
# latest_file = max(onlyfiles,key=os.path.getctime) | |
# latest_file = max(onlyfiles,key=lambda p:p.stat().st_ctime) | |
# print(latest_file) | |
# print (os.path.abspath(__file__)) | |
# print (Path().resolve().parent) | |
# print(onlyfiles) | |
# assert os.path.isfile(onlyfiles[0]) | |
# with open(latest_file,'w') as file: | |
# file = (line.split(",") for line in file) | |
# file.writelines(latest_file) | |
# file = open(latest_file,'r') | |
# Lines = file.readlines() | |
# count = 0 | |
# with open(onlyfiles) as file: | |
# while True: | |
# count += 1 | |
# line = file.readline() | |
# if not line: | |
# break | |
# print("Line{}:{}".format(count,line.strip())) | |
# for line in Lines: | |
# count +=1 | |
# print("Line{}: {}".format(count,line.strip())) | |
# print(file) | |
# super(*args,**kargs).save() | |
# files = onlyfiles.split(",") | |
# Open a file | |
# path= (str(BASE_DIR/ 'templates')) | |
# dirs = os.listdir( path ) | |
# print (dirs) | |
# This would print all the files and directories | |
# for file in dirs: | |
# print(file) | |
# def save(self,*args,**kargs): | |
# with open('xml/','a+') as file: | |
# super(*args,**kargs).save() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment