I want to create a decrypt script in python, my function filename = allfiles(); return the path where is the files , but I try to decrypt my files launch me the error like this below. How can I fix it? I am using python 2.7
Traceback (most recent call last):
File "F:\bug_bounty\decrypt.py", line 41, in <module>
if os.path.basename(filename).startswith("(encrypted)"):
File "C:\Python27\lib\ntpath.py", line 208, in basename
return split(p)[1]
File "C:\Python27\lib\ntpath.py", line 180, in split
d, p = splitdrive(p)
File "C:\Python27\lib\ntpath.py", line 116, in splitdrive
normp = p.replace(altsep, sep)
AttributeError: 'list' object has no attribute 'replace'
Code:
from Crypto.Hash import SHA256
from Crypto.Cipher import AES
import os
import random
import sys
def decrypt(key, filename):
outFile = os.path.join(os.path.dirname(filename),
os.path.basename(filename[11:]))
chunksize = 64 * 1024
with open(filename, 'rb') as infile:
filesize = infile.read(16)
IV = infile.read(16)
decryptor = AES.new(key, AES.MODE_CBC, IV)
with open(outFile, 'wb') as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(decryptor.decrypt(chunk))
outfile.truncate(int(filesize))
def allfiles():
allFiles = []
for (root, subfiles, files) in os.walk(os.getcwd()):
for names in files:
allFiles.append(os.path.join(root, names))
return allFiles
password = 'M4st3rRul3zs'
filename = allfiles();
for files in filename:
if os.path.basename(filename).startswith("(encrypted)"):
print "%s is already encrypted" %filename
pass
else:
decrypt(SHA256.new(password).digest(), filename)
print "Done decrypting %s" %filename
"""os.remove(filename)"""
Related
This question already has answers here:
Trying to use open(filename, 'w' ) gives IOError: [Errno 2] No such file or directory if directory doesn't exist
(3 answers)
Closed 6 months ago.
So my prof. gave me this code as the solution of my homework but when I run it it gives me an error. Can you please help me out? I guess I didn't specify the location of the file but not sure if that's the case.The objective of this question is to generate and read files that contain a list of random numbers.
import random
import os
import time
def fillFile(fileSize, fileName):
# Delete file if exists
if os.path.exists(fileName):
os.remove(fileName)
# Open file
FILE = open(fileName, "w")
# Write to file
for i in range(fileSize):
r = random.randint(0,fileSize+1000)
FILE.write(str(r) + "\n")
FILE.close()
def readFile(fileName):
# Open file
if os.path.exists(fileName):
FILE = open(fileName,"r")
else:
print(fileName + " does not exist!")
exit()
# Read File
alist = []
for line in FILE:
alist.append(int(line))
FILE.close()
return alist
def mainForFiles():
# Dosyaları oluştur
fileSizes = [1000, 5000, 10000, 25000, 50000, 100000, 200000]
dirName = ".\\filesForAssignment1\\"
# Delete fileStats.txt file if exists
statFileName = "fileStats.txt"
if os.path.exists(statFileName):
os.remove(statFileName)
# open stat file
statFile = open(statFileName, "w")
statFile.write("fillFile")
print("WRITING TO FILES")
for i in fileSizes:
start = time.time()
fillFile(i, dirName+"file"+str(i))
finish = time.time()
statFile.write(" " + str(finish-start))
print("File Size = " + str(i) + " Write Time = " + str(finish-start))
statFile.write("\n")
print("READING FILES")
statFile.write("readFile")
for i in fileSizes:
fileName = dirName+"file"+str(i)
# Dosyayı oku
finish = time.time()
alist = readFile(fileName)
start = time.time()
statFile.write(" " + str(finish-start))
print ("File Size = " + str(i)+ " Dosya Okuma Zamanı = " + str(finish-start))
statFile.write("\n")
statFile.close()
mainForFiles()
File "C:/Users/emrea/PycharmProjects/helloworld/hello2.py", line 84, in
<module>
mainForFiles()
File "C:/Users/emrea/PycharmProjects/helloworld/hello2.py", line 57, in mainForFiles
fillFile(i, dirName+"file"+str(i))
File "C:/Users/emrea/PycharmProjects/helloworld/hello2.py", line 12, in fillFile
FILE = open(fileName, "w")
FileNotFoundError: [Errno 2] No such file or directory: '.\\filesForAssignment1\\file1000'
FileNotFoundError: [Errno 2] No such file or directory: '.\\filesForAssignment1\\file1000'
The w mode causes the file to be created if it doesn't exist (and truncated if it does so the os.remove is not actually useful there), however it does expect intermediate directories to exist.
This means you should ensure the path to the file ('.\\filesForAssignment1) does exist before trying to create the file.
os.makedirs(os.path.dirname(fileName), exists_ok=True)
should do the trick, or
pathlib.Path(fileName).parent.mkdir(parents=True, exists_ok=True)
for a somewhat more modern take on it.
There's a bunch of other minor issues in the script:
the main function should generally be "gated" so modules can be imported without running them
explicitly closing files has fallen out of favor as it's unreliable
when opening files in "text" mode (the default) you should always provide an encoding
pathlib is fun, also that way you should not have to deal with path separators and all that crap
unless it's required to handle that case, I'd just let open(fname, 'r') error out if the file doesn't exist
Here's a version I think should be slightly improved:
import pathlib
import random
import os
import time
def fillFile(fileSize, fileName):
with fileName.open('w', encoding='utf-8') as f:
for i in range(fileSize):
r = random.randint(0,fileSize+1000)
f.write(f"{r}\n")
def readFile(fileName):
with fileName.open(encoding='utf-8') as f:
return [int(line) for line in f]
OUT_DIR = pathlib.Path.cwd().joinpath("filesForAssignment1")
FILE_SIZES = [1000, 5000, 10000, 25000, 50000, 100000, 200000]
def mainForFiles():
# Dosyaları oluştur
OUT_DIR.mkdir(parents=True, exist_ok=True) # make sure the directory exists
statFilePath = pathlib.Path("fileStats.txt")
with statFilePath.open('w', encoding='utf-8') as statFile:
statFile.write("fillFile")
print("WRITING TO FILES")
for i in FILE_SIZES:
start = time.time()
fillFile(i, OUT_DIR.joinpath(f'file{i}'))
finish = time.time()
statFile.write(f" {finish-start}")
print(f"File Size = {i} Write Time = {finish-start})")
statFile.write("\n")
print("READING FILES")
statFile.write("readFile")
for i in FILE_SIZES:
f = OUT_DIR.joinpath(f'file{i}')
# Dosyayı oku
start = time.time()
alist = readFile(f)
finish = time.time()
statFile.write(f" {finish-start}")
print (f"File Size = {i} Dosya Okuma Zamanı = {finish-start}")
statFile.write("\n")
if __name__ == '__main__':
mainForFiles()
exit() is not doing what you want, it continues with the code.
def readFile(fileName):
# Open file
if os.path.exists(fileName):
FILE = open(fileName,"r")
else:
print(fileName + " does not exist!")
return
# Read File
alist = []
for line in FILE:
alist.append(int(line))
FILE.close()
return alist
I'm new in python and trying to make script, which renames all files in directory by this pattern ShowName + SeasonNumber + EpisodeNumber
This script gets filename as argv[1] and directory as argv[2]. Then in find_needed_parts it parses string and returns number of seasone, episode and show name. But this script is droping with such traceback:
Traceback (most recent call last):
File "/home/usrname/Documents/renameFiles/main.py", line 38, in <module>
main()
File "/home/usrname/Documents/renameFiles/main.py", line 35, in main
os.rename(pathAndFilename, os.path.join(dir, rename_name))
File "/usr/lib/python3.5/posixpath.py", line 89, in join
genericpath._check_arg_types('join', a, *p)
File "/usr/lib/python3.5/genericpath.py", line 143, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'builtin_function_or_method'
And here is the code:
#!/usr/bin/python3
import glob
import os
import sys
import re
def find_needed_parts(filename):
ep_reg = re.search("(E|e)(\d+)", filename)
if ep_reg:
found_ep = ep_reg.group(2)
s_reg = re.search("(S|s)(\d+)", filename)
if s_reg:
found_s = s_reg.group(2)
ext_reg = re.search("\.(\w+)", filename)
if ext_reg:
ext = ext_reg.group(1)
body_reg = re.search("((^(.*?)([^E0-9])+)|(^(.*?)([^e0-9])))", filename)
if body_reg:
body = body_reg.group(1)
return body, found_ep, found_s, ext
def main():
filename = sys.argv[1]
direct = sys.argv[2]
body, ep, s, ext = find_needed_parts(filename)
pattern = "*." + ext
for pathAndFilename in glob.iglob(os.path.join(direct, pattern)):
title, ext = os.path.splitext(os.path.basename(pathAndFilename))
ep = int(ep) + 1 # type: int
rename_name = str(body + 'S' + s + 'E' + str(ep) + '.' + ext)
os.rename(pathAndFilename, os.path.join(dir, rename_name))
main()
UPD1.
here are the params
argv[1] = abcd E01 S09 ockoeko ko k.avi
argv[2] = .
the main problem is in the statement
os.rename(pathAndFilename, os.path.join(dir, rename_name))
where you are using dir built-in function, when you've probably meant
os.rename(pathAndFilename, os.path.join(direct, rename_name))
I am trying to do a script to find files that contain a text string. The files are on a remote host but when I run it, I get an error
Traceback (most recent call last): File "dirsearch.py", line 55, in
fo = open(search_path + fname) IOError: [Errno 2] No such file or directory: u'/home/black/white/goto/reports/dbs-01-Apr-2017.log'
The script I used is below.
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import
from builtins import input
from builtins import open
from future import standard_library
standard_library.install_aliases()
import pysftp
#connect to sftp server
srv = pysftp.Connection(host="host", username="username",
password="password")
#acess remote directory on server
search_path = ('/home/black/white/goto/reports/')
file_type = '.log'
search_str = input("Enter the search string : ")
#addition ........or fname in os.listdir(path=search_path):
for fname in srv.listdir(search_path):
# Apply file type filter
if fname.endswith(file_type):
# Open file for reading
fo = open(search_path + fname)
# Read the first line from the file
line = fo.readline()
# Initialize counter for line number
line_no = 1
# Loop until EOF
while line != '' :
# Search for string in line
index = line.find(search_str)
if ( index != -1) :
print(fname, "[", line_no, ",", index, "] ", line, sep="")
# Read next line
line = fo.readline()
# Increment line counter
line_no += 1
# Close the files
fo.close()
srv.close()
I've used command-line arguments with argparse many times, but I can't seem to figure out why I am getting a TypeError when trying to use these arguments. I have written a script that will take 5 required arguments. I've included the script below. The argument values are just several directories or file paths. Any idea what I am doing wrong here?
Here is the traceback:
Traceback (most recent call last):
File "C:/program.py", line 186, in <module>
sys.exit(builder.main())
File "C:/program.py", line 119, in main
filename_config = os.path(self.config)
TypeError: 'module' object is not callable
Here is the code:
import argparse
import sys
import os
import re
from tempfile import mkstemp
from shutil import move
from os import remove, close
class Builder():
def __init__(self):
parser = argparse.ArgumentParser(description='builder: ' + 'version 1.0.0')
parser.add_argument('-s', '--source', help="source dir", type=str)
parser.add_argument('-t', '--target', help="destination dir.", type=str)
parser.add_argument('-l', '--lua', help="path and filename of lua code", type=str)
parser.add_argument('-c', '--config', help="path and filename of configuration", type=str)
parser.add_argument('-d', '--documentation', help="path and filename of documentation", type=str)
args = parser.parse_args()
self.source = args.source
self.target = args.target
self.lua = args.lua
self.config = args.config
self.documentation = args.documentation
if not os.path.isdir((self.target)):
os.makedirs(self.target)
def replace_lua(self, filename, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(os.path.join(self.source, filename))
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Move new file
move(abs_path, os.path.join(self.target, filename))
def replace_version(self, filename, pattern, subst):
old_file_path = os.path.join(self.target, filename)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(old_file_path)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(old_file_path)
#Move new file
move(abs_path, os.path.join(self.target, filename))
def replace_documentation(self, filename, pattern, subst):
old_file_path = os.path.join(self.target, filename)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(old_file_path)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(old_file_path)
#Move new file
move(abs_path, os.path.join(self.target, filename))
def main_XXX(self):
if not os.path.isdir(self.target):
os.makedirs(self.target)
#lua pattern
pattern = "<script><![CDATA[]]></script>"
filename_lua = os.path(self.lua)
with open(filename_lua, 'r') as f:
read_lua = f.read()
f.closed
subst = "<script><![CDATA[\n" + read_lua + "\n]]></script>"
#version pattern
old_version = "<version>1</version>"
new_version = "<version>2</version>"
#documentation
old_doc = "<documentation></documentation>"
filename_doc = os.path(self.documentation)
with open(filename_doc, 'r') as f:
read_doc = f.read()
f.closed
read_doc = "<documentation>\n" + read_doc + "\n</documentation>"
for subdir, dirs, files in os.walk(self.source):
for file in files:
print os.path.join(subdir, file)
#file_path = os.path.join(subdir, file)
self.replace_lua(file, pattern, subst)
#replace_version(file, old_version, new_version)
self.replace_documentation(file, old_doc, read_doc)
def main(self):
#create expression objects
version = re.compile(r"^(\s*)<version>.*</version>\s*")
mod_date = re.compile(r"^(\s*)<modified>.*</modified>\s*")
lua_script = re.compile(r"^(\s*)<script>.*</script>\s*")
doc = re.compile(r"^(\s*)<documentation>.*</documentation>\s*")
#get version
filename_config = os.path(self.config)
with open(filename_config, 'r') as f:
for line in f:
m_obj = version.search(line)
if m_obj:
new_version = line
else:
m_obj = mod_date.search(line)
if m_obj:
new_mod_date = line + "\n"
f.closed
filename_doc = os.path(self.documentation)
with open(filename_doc, 'r') as f:
new_doc = f.read()
f.closed
new_doc = "<documentation>\n" + new_doc + "\n</documentation>\n"
filename_lua = os.path(self.lua)
with open(filename_lua, 'r') as f:
new_lua = f.read()
f.closed
#iterate
for subdir, dirs, files in os.walk(self.source):
for file in files:
print os.path.join(subdir, file)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(os.path.join(self.source, file))
for line in old_file:
m_obj = version.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_version)
else:
m_obj = mod_date.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_mod_date)
else:
m_obj = doc.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_doc)
else:
m_obj = lua_script.search(line)
if m_obj:
new_file.write(m_obj.group(1) + "<script><![CDATA[\n" + new_lua + "\n]]></script>\n")
else:
new_file.write(line)
#close temp file
new_file.close()
close(fh)
old_file.close()
#Move new file
move(abs_path, os.path.join(self.target, file))
if __name__ == "__main__":
builder = Builder()
sys.exit(builder.main())
path is a module within os module. You need to call it's member functions. I don't know which one exactly do you need, but there is a os.path module documetnation here: https://docs.python.org/2/library/os.path.html
I've tried the script below:
import os
from ftplib import FTP
ftp = FTP("ftpsite","myuser", "mypass")
ftp.login()
ftp.retrlines("LIST")
ftp.cwd("folderOne")
ftp.cwd("subFolder")
listing = []
ftp.retrlines("LIST", listing.append)
words = listing[0].split(None, 8)
filename = words[-1].lstrip()
#download the file
local_filename = os.path.join(r"C:\example", file)
lf = open(local_filename, "wb")
ftp.retrbinary("RETR " + filename, lf.write, 8*1024)
lf.close()
But everytime I run the script, it says:
Traceback (most recent call last):
File "C:\User\Desktop\sample\ex.py", line 4, in <module>
ftp = FTP("ftpsite", "myuser", "mypass")
File "C:\Python27\lib\ftplib.py", line 119, in __init__
self.login(user, passwd, acct)
File "C:\Python27\lib\ftplib.py", line 387, in login
resp = self.sendcmd('USER ' + user)
File "C:\Python27\lib\ftplib.py", line 244, in sendcmd
return self.getresp()
File "C:\Python27\lib\ftplib.py", line 219, in getresp
raise error_perm, resp
error_perm: 530 Permission denied.
I don't know what 530 Permission Denied means.Can anyone tell me what does that means?
It seems like the ftp server allows anonymous access; You don't need pass username, password.
FTP constructor accept hostname(or IP), not URL.
import sys
import os
from ftplib import FTP
ftp = FTP("ftpsite.com")
ftp.login()
ftp.cwd("/ftp/site/directory/")
listing = []
ftp.retrlines("LIST", listing.append)
words = listing[0].split(None, 8)
filesize = int(words[4])
filename = words[-1].lstrip()
class VerboseWriter:
def __init__(self, lf, filesize):
self.progress = 0
self.lf = lf
self.filesize = filesize
def write(self, data):
self.lf.write(data)
self.progress += len(data)
sys.stdout.write('\r{}/{} ({:.1%})'.format(self.progress, self.filesize, float(self.progress)/self.filesize))
sys.stdout.flush()
#download the file
with open(os.path.join(r"c:\example", filename), 'wb') as f:
ftp.retrbinary("RETR " + filename, VerboseWriter(lf, filesize).write, 8*1024)
print
ftp.quit()