So I've made a script that would delete files with a specific file extension, but how would I go about adding secure deletion of the file?
import sys
import os
from os import listdir
dir_name = "storage/emulated/0/Download/"
test = os.listdir(dir_name)
for file in test:
if file.endswith(".zip"):
os.remove(os.path.join(dir_name, file))
Related
I am very new to Python and am trying to append some functionality to an existing Python program. I want to read values from a config INI file like this:
[Admin]
AD1 = 1
AD2 = 2
RSW = 3
When I execute the following code from IDLE, it works as ist should (I already was able to read in values from the file, but deleted this part for a shorter code snippet):
#!/usr/bin/python
import ConfigParser
# buildin python libs
from time import sleep
import sys
def main():
print("Test")
sleep(2)
if __name__ == '__main__':
main()
But the compiled exe quits before printing and waiting 2 seconds. If I comment out the import of ConfigParser, exe runs fine.
This is how I compile into exe:
from distutils.core import setup
import py2exe, sys
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1}},
zipfile = None,
console=['Test.py'],
)
What am I doing wrong? Is there maybe another way to read in a configuration in an easy way, if ConfigParser for some reason doesnt work in a compiled exe?
Thanks in advance for your help!
I'm using python 2.7 Wrapper of Mega Api on Ubuntu. With the following code I could upload a set of files to my Mega Account, however I'd like to know how I can upload files to a specific folder on my account.
from mega import Mega
import os
from os import listdir
from os.path import isfile, join
mega = Mega()
mega._login_user('email','password')
def absoluteFilePaths(directory):
for dirpath,_,filenames in os.walk(directory):
for f in filenames:
yield os.path.abspath(os.path.join(dirpath, f))
directory = '/home/caioignm/test_folder'
file_path_generator = absoluteFilePaths(directory)
for file_path in file_path_generator:
mega.upload(file_path)
On my Mega Account, I have one folder, 'Personal', with other folder inside, 'Vacations', where all my files are stored. When I upload without refering a destination path, the files are stored in root directory.
I tried to follow help instructions of mega package, but I didn't find out how to set destination folder of my files.
Help on method upload in module mega.mega:
upload(self, filename, dest=None, dest_filename=None) method of mega.mega.Mega instance
##########################################################################
# UPLOAD
Doing mega.upload('filename', 'Personal/Vacations') I haven't received any error message, but my files were not uploaded too
It was so easy, like this:
Folder = mega.find('my_mega_folder')
mega.upload('yourfile.txt', Folder[0])
I have added it to your code:
from mega import Mega
import os
from os import listdir
from os.path import isfile, join
mega = Mega()
mega._login_user('email','password')
def absoluteFilePaths(directory):
for dirpath,_,filenames in os.walk(directory):
for f in filenames:
yield os.path.abspath(os.path.join(dirpath, f))
directory = '/home/caioignm/test_folder'
file_path_generator = absoluteFilePaths(directory)
Folder = mega.find('your_folder') #change it with the folder in your mega
for file_path in file_path_generator:
mega.upload(file_path, Folder[0])
Upload a file to a destination folder:
folder = m.find('my_mega_folder')
m.upload('myfile.doc', folder[0])
Source : https://pypi.org/project/mega.py/
Hello sorry if this question has been asked before.
But I have tried a lot of methods that provided.
Basically, I want to download the file from a website, which is I will show my coding below. The code works perfectly, but the problem is the file was auto download in our download folder path directory.
My concern is to download the file and save it to a specific folder.
I'm aware we can change our browser setting since this was a server that will remote by different users. So, it will automatically download to their temporarily /users/adam_01/download/ folder.
I want it to save in server disk which is, C://ExcelFile/
Below are my script and some of the data have been changing because it is confidential.
import pandas as pd
import html5lib
import time from bs4
import BeautifulSoup
import requests
import csv
from datetime
import datetime
import urllib.request
import os
with requests.Session() as c:
proxies = {"http": "http://:911"}
url = 'https://......./login.jsp'
USERNAME = 'mwirzonw'
PASSWORD = 'Fiqr123'
c.get(url,verify= False)
csrftoken = ''
login_data = dict(proxies,atl_token = csrftoken, os_username=USERNAME, os_password=PASSWORD, next='/')
c.post(url, data=login_data, headers={"referer" : "https://.....com"})
page = c.get('https://........s...../SearchRequest-96010.csv')
location = 'C:/Users/..../Downloads/'
with open('asdsad906010.csv', 'wb') as output:
output.write(page.content )
print("Done!")
Thank you, be pleased to ask if any confusing information was given.
Regards,
Fiqri
It seems that from your script you are writing the file to asdsad906010.csv. You should be able to change the output directory as follows.
# Set the output directory to your desired location
output_directory = 'C:/ExcelFile/'
# Create a file path by joining the directory name with the desired file name
file_path = os.path.join(output_directory, 'asdsad906010.csv')
# Write the file
with open(file_path, 'wb') as output:
output.write(page.content)
I have a flask application that I would like to convert into an executable for deploying elsewhere. I have used py2exe for that. I am getting jinja2:TemplateNotFound error. I have copied the static and templates folders into the dist folder where the exe files reside. Pls let me know if I am missing something. My setup file is as follows:
from distutils.core import setup
import py2exe
import os
from glob import glob
import sys
from distutils.filelist import findall
import matplotlib
matplotlibdatadir = matplotlib.get_data_path()
matplotli bdata = findall(matplotlibdatadir)
matplotlibdata_files = []
for f in matplotlibdata:
dirname = os.path.join('matplotlibdata', f[len(matplotlibdatadir)+1:])
matplotlibdata_files.append((os.path.split(dirname)[0], [f]))
data_files=[('static', glob("D:\\pythonLearning\\static\\*.*")), ('templates', glob("D:\\pythonLearning\\templates\\login.html"))]
data_files.extend(matplotlibdata_files)
print data_files
sys.path.append('C:\\Windows\\winsxs\\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57')
setup( console=['myfile.py'],
options={ 'py2exe': { 'packages' : ['matplotlib', 'pytz','werkzeug','email','jinja2.ext'],
'includes': ['flask','jinja2'] } },
data_files=data_files )
This is because jinja expects your egg to be unzipped and available via the filepath. See for more information.
You can workaround this for typical data files using this
but jinja2 has no direct support for this and you would have to implement this yourself
I have a notepad file in my PC with path D:/example.txt,how can i display this file using my python code.
If you mean open the file with notepad:
Using os.startfile (only available in Windows):
import os
os.startfile(r'd:\example.txt')
Using subprocess.Popen or subprocess.call:
import subprocess
subprocess(['notepad', r'd:\example.txt'])
To print to console
import sys
with open(r'D:\example.txt') as f:
sys.stdout.writelines(f)
with open(r'D:\example.txt') as f:
for line in f:
print line.rstrip('\n')