Ok so... I'm trying to make an discord bot as some of you know, and I want to organize the logs while they are created so... for example... when I open the bot file to run (draco.py) it creates an file called 1_logs and when I close it keeps saved and when I open the file to the bot to run it makes the file 2_logs
Is that possible? Ive tried creating another file that counts what log number is with file.read() and file.write(), but I didnt had any success...
Is that possible?
If you are keeping the log files in a folder, you can easily search through your folder using either os, sys or glob modules. Then you can sort them.
But still what #ForceBru suggested is easier.
For example:
glob.glob('*_logs') # will give you all the log files you have.
Related
I am working on a bot that downloads a file using python and selenium webdriver. I have successfully been able to design the bot and it is working perfectly. The issue is
I want to target this file that was just downloaded. This program is going to be used by many people who may have set different directory for downloads. How do I use the OS module to achieve this?
Once the data is downloaded from the site, the default name is transactions; my users may have other files with the same name and my program might copy a wrong file. The file is in an excel format.
How do I target this file alone and not any other file even if they have the same name or is there a way I can change the directory my bot save any file it downloads so I don't need to copy to the directory where I want it for calculations?
consider use of shutil
newfilename = os.path.join(os.path.abspath('foopath'), 'foofile') shutil.copyfile('transactions', newfilename)
Reference other options at
copy-a-file-from-one-location-to-another-in-python
[Python 2.6.5] Simple question but can't seem to find any documentation or previous questions answering my problem. I'm trying to delete 2 files within a ZIP Folder (Its 2 Folders deep in the ZIP Folder if that matters). I want to use subprocess to call 7Zip to delete the files but have no clue how to even start that process. As of now I can only open the application. Ideally, I'd like to run the python script, have the file names hard coded into the script, and have 7zip just automatically delete those files without the application ever opening if possible.
What I have so far:
import subprocess, os
rawLocation = 'SomeLocation\7-ZipPortable.exe'
subprocess.Popen([rawLocation])
This successfully opens the application, but I'd like to run and have it automatically delete files without even opening up if possible.
Note: Thanks for the help everyone. The customer's requirements ended up changing so I don't need to use this logic anymore.
You need to use the command line version of 7zip. And then according to this documentation, you should be able to delete files like so.
7z d archive.zip deleteme.ext
Note: for anyone who might think that Python's zipfile module would be a good solution, I looked through the module, and it doesn't look like it supports deleting files from an archive.
something like this :
from subprocess import call
delete_me = "note.txt"
zip_file = "archive.zip"
try:
call(['7z d', zip_file, delete_me])
except OSError:
# handle errors here
pass
my program creates a log file on the users desktop using:
file = open(os.path.expanduser("~/Desktop/Log.txt"), 'a')
But later I need to send the log file back to myself using MIME. To send a file it needs the the file directory but I do not know how to find that? I tried doing this:
filename='(os.path.expanduser("~/Desktop/Log.txt"), 'a')'
But this does not work. Is there another way to do this? If there is then please give an example since I am a beginner and don't really know what im doing.
Did you try this one? See if it works.
directory=os.path.dirname(os.path.expanduser("~/Desktop/Log.txt"))
if you want absolute path try
fullpath=os.path.abspath(os.path.expanduser("~/Desktop/Log.txt"))
more convenient functions here:
https://docs.python.org/3/library/os.path.html
I am trying to list (let's say) PDF files in a directory (to a GUI). I know I can manage that with in different ways (like below) but I also want the list is to be current and dynamic. For instance, if someone deletes a PDF file from or adds a PDF file to the directory, I want the application to delete or add the file name to the list instantly, without any refresh.
Do you know any ways to manage that?
Here is some of my code:
import os
pdffiles = [name for name in os.listdir('somedir')
if name.endswith('.pdf')]
for file in pdffiles:
print(file)
I am in a learning progress.
Your code looks good so far. If you want to do this programmatically (avoiding pynotify or signal handling), the simplest way to maintain a directory listing in your application is via polling the directory.
So, your code might look like:
import os
import time
## run indefinitely
while True:
## get the directory listing
pdffiles = [name for name in os.listdir('somedir')
if name.endswith('.pdf')]
## print the most recent listing
for file in pdffiles:
print(file)
## wait a little bit before getting the next listing
## if you want near instantaneous updates, make the sleep value small.
time.sleep(1)
You need to get notified by the OS that the directory and/or some files changed.
On Linux you can use pyinotify
On OSX you can do that with MACFSEvents
On Windows (.Net) you can try to interface with FileSystemWatcher, but I am not aware of any specific libraries for that.
And a platform independent solutions is watchdog
Hooking that into your program is non-trivial, but the examples of watchdog are clear and that means there is no need for polling.
I have a simple web-server written using Python Twisted. Users can log in and use it to generate certain reports (pdf-format), specific to that user. The report is made by having a .tex template file where I replace certain content depending on user, including embedding user-specific graphs (.png or similar), then use the command line program pdflatex to generate the pdf.
Currently the graphs are saved in a tmp folder, and that path is then put into the .tex template before calling pdflatex. But this probably opens up a whole pile of problems when the number of users increases, so I want to use temporary files (tempfile module) instead of a real tmp folder. Is there any way I can make pdflatex see these temporary files? Or am I doing this the wrong way?
without any code it's hard to tell you how, but
Is there any way I can make pdflatex see these temporary files?
yes you can print the path to the temporary file by using a named temporary file:
>>> with tempfile.NamedTemporaryFile() as temp:
... print temp.name
...
/tmp/tmp7gjBHU
As commented you can use tempfile.NamedTemporaryFile. The problem is that this will be deleted once it is closed. That means you have to run pdflatex while the file is still referenced within python.
As an alternative way you could just save the picture with a randomly generated name. The tempfile is designed to allow you to create temporary files on various platforms in a consistent way. This is not what you need, since you'll always run the script on the same webserver I guess.
You could generate random file names using the uuid module:
import uuid
for i in xrange(3):
print(str(uuid.uuid4()))
The you save the pictures explictly using the random name and pass insert it into the tex-file.
After running pdflatex you explicitly have to delete the file, which is the drawback of that approach.