file modifiaction and manipulation

2024/7/4 15:51:14

How would you scan a dir for a text file and read the text file by date modified, print it to screen having the script scan the directory every 5 seconds for a newer file creadted and prints it. Is it possible that you can help me i'm stuck and i need this real bad and i've already got the scan dir for file and print but it does not print the files by date modidfied.

import os,sys
os.chdir(raw_input('dir_path: ') )    
contents=os.listdir('.') #contents of the current directory
files =[]
directory=[]
Time = time.ctime(os.path.getmtime(contents))
for i in contents:if os.path.isfile(i) == True :files.append(i)elif os.path.isdir(i) == True :directory.append(i)#printing contents
choice = ""       
for j in files:while choice != "quit":choice = raw_input("Dou you want to print file  %s (y/n): "%j)if choice == 'y':print "**************************"print "Printing Files %s" %jprint "**************************"fileobj = open(j,'r')contents = fileobj.readlines()for k in contents:sys.stderr.write(k)else:pass

what i wanted is instead of my code asking if it wants to print i need it to print the files if modified by the current time meaning if it read a file that was just placed in the directory and a new one comes in it will read the new file without prompting me. the error it's giving me is coercing to unicode: need string or buffer, list found.

Answer

Repeating actions on a timer

You can repeat an action every five seconds by combining an infinite loop with the time.sleep() function, like so:

import time
while True:time.sleep(5)         # wait five secondsprint (time.time())   # print the time

Remember to have some kind of break condition in here if you need it, otherwise the loop will run forever.

"TypeError: coercing to Unicode: need string or buffer, list found"

Your problem is in the line

Time = time.ctime(os.path.getmtime(contents))

You have provided a list of filenames. The os.path.getmtime function expects one filename at a time. The error message is telling you that it has no idea how to convert a list of filenames into a filename.

https://en.xdnf.cn/q/119650.html

Related Q&A

Get quantitative value for color on two-color scale

I have run a chemical test that produces a color depending on how much of a given chemical is in the sample. It is green if there is no chemical, and yellow if there is a saturating amount of chemical.…

how to save python session input and output [duplicate]

This question already has answers here:How to save a Python session, including input and output, as a text?(4 answers)Closed 2 years ago.All of the ways which discussed this question save the history …

flask_mysqldb Delete FROM variable table [duplicate]

This question already has answers here:Python sqlite3 parameterized drop table(1 answer)How do I use SQL parameters with python?(1 answer)Closed 6 years ago.So i use flask_mysqldb in a Flask(Python we…

Syntax Error at the end of a while loop

EDIT: This question was ask at the start of my learning process for python. The Syntax Error was produced by pythons IDLE with no trackback to speak of. This was the main cause of the problem and confu…

Creating an adjacency list class in Python

I was wondering how to create an adjacency list class Here is what I have so far:class AdjNode:def __init__(self, value):self.vertex = valueself.next = Noneclass Graph:def __init__(self):# Add edgesdef…

How can I separate a rust library and the pyo3 exported python extensions which wrap it

I have a rust library which provides useful functionality for use in other rust programs. Additionally I would like to provide this functionality as a python extension (using pyo3 and setuptools-rust, …

how do I count unique words of text files in specific directory with Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python convert path to dict

I have a list of paths that need to be converted to a dict ["/company/accounts/account1/accountId=11111","/company/accounts/account1/accountName=testacc","/company/accounts/acc…

Python: How to download images with the URLs in the excel and replace the URLs with the pictures?

As shown in the below picture,theres an excel sheet and about 2,000 URLs of cover images in the F column. What I want to do is that downloading the pictures with the URLs and replace the URL with the…

I cant figure out pip tensorrt line 17 error

I couldnt install it in any way, I wonder what could be the cause of the error. I installed C++ and other necessary stuff I am using windows 11 I installed pip install nvidia-pyindex with no problem. S…