I'm trying to upload a file with my Slack bot. I can send a file with exact path+name without any problem, for example "C:/Program Files/file.txt". But in this case I want to upload LATEST file (newest one) from given directory (with X txt files), because these .txt files are automatically generated and have added date+time to their name, for example "file 06-02-2023 10:23.txt", so I can't upload them easily!
I used python's code to get latest file from directory:
import os
import glob
dir_path = 'C:/Users/MyUser/Desktop/Files'
list_of_files = glob.glob(os.path.join(dir_path, '*.txt'))
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)
I tried then to use this variable as file/path+file, as shown in code below:
import os
import glob
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
dir_path = 'C:/Users/MyUser/Desktop/Files'
list_of_files = glob.glob(os.path.join(dir_path, '*.txt'))
latest_file = max(list_of_files, key=os.path.getctime)
client = WebClient(token="xoxb-MY-BOT-TOKEN")
client.files_upload(channels="my-channel", file=latest_file, title='Test', filetype='.txt')
but I get error: {"ok":false,"error":"no_file_data"}
I've tried various versions of code above(with little changes), for example:
latest_file = max(list_of_files, key=os.path.getctime)
just_file = os.path.basename(latest_file)
client.files_upload(channels="my-channel", file=f"C:/Users/MyUser/Desktop/Files"+just_file, title='Test', filetype='.txt')
Any change in file path/name etc. didn't work, it's just "error":"no_file_data" all the time...
Is it possible to do? Or maybe such codes aren't allowed and you have to paste just exact full file's path?
Related
import os
import aspose.words as aw
rootdir = 'C:/Users/user/stuff/tests'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
a = os.path.join(subdir, file)
doc = aw.Document(a)
doc.save("utput.docx")
doc = aw.Document("Output.docx")
doc.save("output.pdf")
This is my program.
I am trying to run python through a folder containing pdf files, and decrypt them one by one by converting it to word, and then to pdf. What am I doing wrong?
Don't use os.walk. use os.listdir(rootdir) instead. Please note that the saved file and the used file have the same name.
Example:
import os
import aspose.words as aw
root = "C:/Users/user/stuff/tests"
for item in os.listdir(root):
if os.path.isfile(os.path.join(root, item)):
doc = aw.Document(item)
doc.save("Output.docx")
doc = aw.Document("Output.docx")
doc.save("output.pdf")
[EDIT]
above code cant find other folders so i decide to use glob to find all folders
Here:
import os
import aspose.words as aw
import glob
# Set base directory
os.chdir(os.path.join("C:/Users/user/stuff/tests"))
# Geting all pdf files in list
pdf_files = glob.glob("*.pdf")
for files in pdf_files:
doc = aw.Document(files)
doc.save("Output.docx")
doc = aw.Document("Output.docx")
doc.save("output.pdf")
[EDIT-2]
First take all .pdf files in one list :
pdf_files = glob.glob("*.pdf")
other_pdf_files = glob.glob('*/*.pdf')
all_pdf_files=(*pdf_files,*other_pdf_files)
Secondly, you need to use PyPDF2 to get rid of password.
Get unencrypted pdfs by sending all pdf files into decrypt_pdf (don't forget to specify the password). For example: (More detail here and here)
from PyPDF2 import PdfFileReader, PdfFileWriter
def decrypt_pdf(input_path, output_path, password):
with open(input_path, 'rb') as input_file, \
open(output_path, 'wb') as output_file:
reader = PdfFileReader(input_file)
reader.decrypt(password)
writer = PdfFileWriter()
for i in range(reader.getNumPages()):
writer.addPage(reader.getPage(i))
writer.write(output_file)
You can run other parts in the same way.
for files in all_pdf_files:
doc = aw.Document(files)
...
I am getting stuck when trying to iterate through files in a directory ('PDFS') with fitz from PyMuPDF.
The thing is, the code works when I am just doing document = "somepdf.pdf", but as soon as I insert a for loop and try to access files that way this error shows up:
filename, stream, filetype, rect, width, height, fontsize
RuntimeError: cannot open sample.pdf: No such file or directory
Here is the code:
for file in os.listdir('PDFS'):
if fnmatch.fnmatch(file, '*.pdf'):
document = file
doc = fitz.open(document)
Thank you for the help!
Your pdf files to open is under sub-directory PDFS, e.g. PDFS/sample.pdf, while your code fitz.open(document) is to open file under current working directory. So, a fix should be:
import fitz
import os
import fnmatch
for file in os.listdir('PDFS'):
if fnmatch.fnmatch(file, '*.pdf'):
document = os.path.join('PDFS', file)
doc = fitz.open(document)
Furthermore, a relative path PDFS is used, so you have to run the code right under the path where PDFS in, say /your/workspace/:
/your/workspace > python code.py
Otherwise,
/your > python workspace/code.py
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'PDFS'
So, a good practice is to
use full path if PDFS is just a user input path; otherwise,
use relative path to the script path
script_path = os.path.abspath(__file__)
project_path = os.path.dirname(script_path)
pdfs_path = os.path.join(project_path, 'PDFS')
I had the same issue. Its because PyCharm automatically installs the module named fitz whereas you need to write the following in the terminal:
pip install PyMuPDF
This will automatically install fitz and you can use the function fitz.open()
This code was working before today.
I am using this line to encrypt a file and save it:
gpg.encrypt_file(f,recipients=encrypt_key,output= encrypted_file)
Here is the code:
gpg = gnupg.GPG()
path = 'secure_data' #********** Please make secure_data folder in utils before running
os.chdir(path)
files = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
latest_file = files [-1]
if encrypt_key is not None:
with open (latest_file, 'rb') as f:
encrypted_file = latest_file +".gpg"
gpg.encrypt_file(f,recipients=encrypt_key,output= encrypted_file)
The code throws no errors, but no file is created. It worked previously up until today.
Any thoughts?
I want to get latest 3 ".jpeg" files from a folder using python
I tried like
import os
import glob
path = "path of the folder\\*.jpeg"
list_of_files = glob.iglob(path)
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)
But I got only one file as output. How to get latest 3 files from a folder?
Just sort the list by date and pluck off the last three elements.
import os
import glob
path = "path of the folder\\*.jpeg"
latest_files = sorted(glob.iglob(path), key=os.path.getctime))
print(latest_files[-3:]
Try the following:
import os
import glob
files_path = os.path.join("C:\\Users\\User\\Pycharmprojects\\other\\", '*')
list_of_files = sorted(glob.iglob(files_path), key=os.path.getctime, reverse=True)
list_of_files=[i for i in list_of_files if i[-4:]=='jpeg']
latest_files = list_of_files[:3]
print(latest_files)
The following code works well and gives me the require output with the file path.
import glob
import os
list_of_files = glob.glob('/path/to/folder/*')
latest_file = max(list_of_files, key=os.path.getctime)
print latest_file
But if the file is created then it will give the file path but if the folder is created then it will give the folder path. Whereas I was expecting only the file and not the folder created in a specific folder.
Kindly, suggest what I should do to get only the latest created file path and not the latest created folder path.
you can use something like this using lambdas
filelist = os.listdir(os.getcwd())
filelist = filter(lambda x: not os.path.isdir(x), filelist)
newest = max(filelist, key=lambda x: os.stat(x).st_mtime)
The complete answer is posted here
https://ubuntuforums.org/showthread.php?t=1526010
If you are importing os already, then you don't need any other modules to achieve this. os has a module called path which handles path related functions.
To check if a path is directory or a file, you can check os.path.isfile('/path/here') which will return a boolean true or false depending on if the passed parameter is a file or not
Try this:
import glob
import os
def check_file_status(path,direc=None):
if direc==None:
list_of_files = glob.glob(path)
latest_file = max(list_of_files, key=os.path.getctime)
return latest_file
else:
new_path = direc+'/*'
list_of_files = glob.glob(new_path)
latest_file = max(list_of_files, key=os.path.getctime)
return latest_file
if __name__ =="__main__":
path = '/your path to file/*'
if os.path.isfile(check_file_status(path)):
print(latest_file)
elif os.path.isdir(check_file_status(path)):
add_into_path = check_file_status(path)
print(check_file_status(path,add_into_path))