I have a python script that fails at a specific line of code. I wrote it on Sublime text and had no errors. This is on Python 3.8 & 3.7.
import os
import glob
import time
<lines 4-28 of script are omitted>
list_of_files = glob.glob('Y:\\foldername\\foldername\\Reports\\*.csv')
latest_file = max(list_of_files, key=os.path.getctime)
create_time = os.path.getctime(latest_file)
When I run this script in PyCharm it fails with the following error confirming its that an issue with this specific line of code latest_file = max(list_of_files, key=os.path.getctime)
Here is the error in PyCharm
line 30, in <module>
latest_file = max(list_of_files, key=os.path.getctime) ValueError: max() arg is an empty sequence
If I remove that one line the script runs fine in Windows Task Scheduler and in IDLE and everywhere. I am going to probably just find another way to get the latest file in the specified folder.
Can you guys help me figure out why the script fails? Does the Value error mean I cannot give any parameters to max()? Or point me in the right direction to fix it. Im fairly new to python so this is a learning opportunity!
Related
I use the pywin32 library since one week to parse visio files to extract shapes and their content. I ran my scripts several times but since yesterday when I execute them I have this error :
File "C:\Program Files\Python39\lib\site-packages\win32com\client\__init__.py", line 580, in __getattr__ raise AttributeError( AttributeError: '<win32com.gen_py.Microsoft Visio 16.0 Type Library.IVDocument instance at 0x1943434388768>' object has no attribute 'pages'
Here is the part of my script which generate the error :
import glob
import os
import win32com.client as w32
path = r"C\Users\..."
all_files = glob.glob(path + "/*.vsd")
visio = w32.Dispatch("visio.Application")
for filename in all_files:
print(filename)
vdoc = visio.Documents.Open(filename)
page = vdoc.pages(1) <-- the problematic line
shps = page.Shapes
I make a list of visio files and after I open them in the for loop I read the first page (they have all one page). The first visio open as well but after I have the error.
I tried to uninstall et reinstall pywin32, I worked in another repertory, change my import name... I tried on another PC and pywin works as well.
I really don't understand why python rise this error now, above all I don't touch this line .
Have you some ideas to resolve this problem ?
I don't understand how it works but capitalizing all commands like .Pages/.Type/.Text/.Shapes/.Names because before the commands uncapitatlized .pages/.type/.text/.shapes/.names worked... It's very weird if somebody have an explanation I take it.
I want to get a number from the filepath of the current file in Sikuli - Jython
I have a python example of what i'm trying to achive.
In the example the path is:
C:\PycharmProjects\TestingPython\TestScripts\TestScript_3.sikuli\TestScript.py
import os
PointerLeft = "Script_"
PointerRight = ".sikuli"
FilePath = os.path.dirname(os.path.abspath(__file__))
NumberIWant = FilePath[FilePath.index(PointerLeft) + len(PointerLeft):FilePath.index(PointerRight)]
print(NumberIWant)
So what i want to get is the number 3. In python the example above works, but I cant use the __file__ ref in Sikulix. Nor does the split of the string work, so even if I get the string of the path, I still have to get the number.
Any help and/or ideas is greatly appreciated
Important:
the .py file in a .sikuli folder must have the same name
hence in your case: ...\TestScript_3.sikuli\TestScript_3.py
It looks like you are trying to run your stuff in the PyCharmcontext. If you do not run the script in the SikuliX IDE, you have to add from sikuli import * even to the main script.
To get the file path of the script use getBundlePath().
Then os.path(getBundlePath()).basename() will result to the string "TestScript_3.sikuli".
RaiMan from SikuliX
I am using the following code to try and find the most up to date file in a folder.
However i am getting a funny character back
The actual file is called G:\\foo\\example - YTD.zip
# Identify the file with the latest create timestamp
list_of_files = glob.glob('G:\\foo\\\\*.zip')
latest_file = max(list_of_files, key=os.path.getctime)
latest_file
'G:\\foo\\example � YTD.zip'
Can anyone help?
I am a complete Python newbie so appreciate this could be a rookie question
I have tried this solution:
How to get the latest file in a folder using python
The code I tried is:
import glob
import os
list_of_files = glob.glob('/path/to/folder/**/*.csv')
latest_file = max(list_of_files, key=os.path.getctime)
print (latest_file)
I received the output with respect to the Windows log of timestamp for the files.
But I have maintained a log separate for writing files in the respective sub-folder.
When I opened the log I see that the last updated file was not what the Python code has specified.
I was shocked as my complete process was depending upon the last file written.
Kindly, let me know what I can do to get the last updated file through Python
I want to read the file which is updated last, but as windows is not prioritizing the updation of the file Last modified, I am not seeing any other way out.
Does anyone has any other way to look out for it?
In linux, os.path.getctime() returns the last modified time, but on windows it returns the creation time. You need to use os.path.getmtime to get the modified time on windows.
import glob
import os
list_of_files = glob.glob('/path/to/folder/**/*.csv')
latest_file = max(list_of_files, key=os.path.getmtime)
print (latest_file)
This code should work for you.
os.path.getctime is the creation time of the file - it seems you want os.path.getmtime which is the modification time of the file, so, try:
latest_file = max(list_of_files, key=os.path.getmtime)
and see if that does what you want.
So I'm trying to write a script that allows me import the most recently modified file from a directory. I've looked at the glob and os.listdir commands but they don't seem to do it (I get errors). Any thoughts?
import os
import glob
newest = max(glob.iglob('Directory Name'), key=os.path.getctime)
print newest
f = open(newest,'r')
I get an error:
max() arg is an empty sequence
Would something like os.stat work better?
Try:
newest = max(glob.iglob('Directory Name/*'), key=os.path.getctime)