User Inputted Directory to copy - python

GITHUB
Hi,
This is a pretty basic script i have started in python..
I intend to eventually, in nyears to come lol, to use it to backup from my server(FTP) to my local backup drive (E), then backup again to my other Local backup drive (D:).
So far im buzzing..
I managed to copy using copytree from a directory i specify in the script to another directory that i specify in the script..
Local E: to local D:..
my next task, well next mission is to be able to Input the directory i wish to copy from and copy to..
i assumed this would be as simplae as chanhging the
src =
to
src = input()
alas this causes an error..
anyways here is the script and i would appreciate any input as Python, so fun is still an alien langauge to me lol..
import shutil, os, datetime, time, sys
newFolderName = datetime.datetime.now()
newFolder = newFolderName.strftime('%m%d%Y')
src = input('type the src dir: ')
dst= input('type the dest dir: ' +
newFolderName.strftime('%d-%B-%y\\').upper())
shutil.copytree(src, dst)

Related

PermissionError when copying files using shutil.copyfile

I am a beginner in python and this is my first application.
So, basically, the application takes a folder with mp3 files in it, and reads the metadata for artist name, and then sorts the songs accordingly by copying them into newly created sub-folders named after the artist name. If there's no artist name, it would create a folder called 'unsorted' and copy files into that. I have got to the point of being able to create new folders, but the last bit which is copying the files gives me a PermissionError. Below are the code and the error I am getting.
import os
import eyed3
import shutil
# get the path to music directory
musicFolder = input('please enter the full path of your music folder : ')
correctedPath = musicFolder.replace('/', '//')
musicList = []
# list of audio file objects
for _files in os.listdir(correctedPath):
if _files.endswith('.mp3'):
musicList.append(eyed3.load(correctedPath + '//' + _files))
sortedFolder = ''
# check tag info for album artist, and sort to folders
for _audioFiles in musicList:
if _audioFiles.tag.album_artist != None:
sortedFolder = correctedPath + '//' + _audioFiles.tag.album_artist
else:
sortedFolder = correctedPath + '//' + 'Unsorted'
if not os.path.exists(sortedFolder):
os.mkdir(sortedFolder)
shutil.copyfile(_audioFiles.path, sortedFolder)
Error
PermissionError: [Errno 13] Permission denied: 'C://Users//Manu//Music//Music Test//Coldplay'
Any help is highly appreciated.
Thanks for your time.
It looks to me like you are using forward slash / where you should be using backslash .
'C://Users//Manu//Music//Music Test//Coldplay'
should be
'C:\\Users\\Manu\\Music\\Music Test\\Coldplay'
Better yet, Python has built in libraries to assist with paths: os.path and pathlib.
https://medium.com/#ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f
shutil.copy2(src,dst) fixed it for me. I changed it from shutil.copyfile.
shutil copy functions

Code that only execute once, Python Startup folder

Hey and thanks for all of your answers. I try to write a piece of python code that only executes once, (first time the program is installed) and copies the program into the windows startup folders.
(C:\Users\ USER \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup)
That's the code i wrote for this. (Please don't judge me. I know it's
very shitty code. But I'm very new to coding. (this is the second
little program i try to write)
import os
import shutil
#get username
user = str(os.getlogin())
user.strip()
file_in = ('C:/Users/')
file_in_2 = ('/Desktop/Py Sandbox/test/program.py')
file_in_com = (file_in + user + file_in_2)
folder_seg_1 = ('C:/Users/')
folder_seg_2 = ('/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup')
#create FolderPath
folder_com = (folder_seg_1 + user + folder_seg_2)
shutil.copy2(file_in_com, folder_com)
Because i got an error, that there is no such internal, external,
command, program or batch file named Installer. I tried to generate a batch file with
nothing in it that executes when the installation process is finished.(But the error is still there.)
save_path = 'C:/Windows/assembly/temp'
name_of_file = str("Installer")
completeName = os.path.join(save_path, name_of_file+".bat")
file1 = open(completeName, "w")
file1.close()
The main idea behind this that there is my main Program, you execute
it it runs the code above and copies itself to the startup folder.
Then the code the whole installer file gets deleted form my main
program.
import Installer
#run Installer File
os.system('Installer')
os.remove('Installer.py')
But maybe there's someone out there who knows the answer to this problem.
And as I said earlier, thanks for all of your answers <3.
BTW I'm currently using Python 3.5.
Okay guys now I finally managed to solve this problem. It's actually not that hard but you need to think from another perspective.
This is now the code i came up with.
import os
import sys
import shutil
# get system boot drive
boot_drive = (os.getenv("SystemDrive"))
# get current Username
user = str(os.getlogin())
user.strip()
# get script path
script_path = (sys.argv[0])
# create FolderPath (Startup Folder)
folder_seg_1 = (boot_drive + '/Users/')
folder_seg_2 = ('/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup')
folder_startup = (folder_seg_1 + user + folder_seg_2)
#check if file exits, if yes copy to Startup Folder
file_name = (ntpath.basename(script_path))
startup_file = (folder_startup + ("/") + file_name)
renamed_file = (folder_startup + ("/") + ("SAMPLE.py"))
# checkfile in Startup Folder
check_path = (os.path.isfile(renamed_file))
if check_path == True:
pass
else:
shutil.copy2(script_path, folder_startup)
os.rename(startup_file, renamed_file)
This script gets your username, your boot drive, the file location of
your file than creates all the paths needed. Like your personal
startup folder. It than checks if there is already a file in the
startup folder if yes it just does nothing and goes on, if not it
copies the file to the startup folder an than renames it (you can use that if you want but you don't need to).
It is not necessary to do an os.getenv("SystemDrive") or os.getlogin(), because os.getenv("AppData") already gets both. So the most direct way of doing it that I know of is this:
path = os.path.join(os.getenv("appdata"),"Microsoft","Windows","Start Menu","Programs","Startup")

Opening Pathway to Remote Directory (Python)

I am working on a code to copy images from a folder in a local directory to a remote directory. I am trying to use scp.
So in my directory, there is a folder that contains subfolders with images in it. There are also images that are in the main folder that are not in subfolders. I am trying to iterate through the subfolders and individual images and sort them by company, then make corresponding company folders for those images to be organized and copied onto the remote directory.
I am having problems creating the new company folder in the remote directory.
This is what I have:
def imageSync():
path = os.path.normpath("Z:\Complete")
folders = os.listdir(path)
subfolder = []
#separates subfolders from just images in complete folder
for folder in folders:
if folder[len(folder)-3:] == "jpg":
pass
else:
subfolder.append(folder)
p = dict()
for x in range(len(subfolder)):
p[x] = os.path.join(path, subfolder[x])
sub = []
for location in p.items():
sub.append(location[1])
noFold= []
for s in sub:
path1 = os.path.normpath(s)
images = os.listdir(path1)
for image in images:
name = image.split("-")
comp = name[0]
pathway = os.path.join(path1, image)
path2 = "scp " + pathway + " blah#192.168.1.10: /var/files/ImageSync/" + comp
pathhh = os.system(path2)
if not os.path.exists(pathhh):
noFold.append(image)
There's more to the code, but I figured the top part would help explain what I am trying to do.
I have created a ssh key in hopes of making os.system work, but Path2 is returning 1 when I would like it to be the path to the remote server. (I tried this: How to store the return value of os.system that it has printed to stdout in python?)
Also how do I properly check to see if the company folder in the remote directory already exists?
I have looked at Secure Copy File from remote server via scp and os module in Python and How to copy a file to a remote server in Python using SCP or SSH? but I guess I am doing something wrong.
I'm new to Python so thanks for any help!
try this to copy dirs and nested subdirs from local to remote:
cmd = "sshpass -p {} scp -r {}/* root#{}://{}".format(
remote_root_pass,
local_path,
remote_ip,
remote_path)
os.system(cmd)
don't forget to import os,
You may check the exitcode returned (0 for success)
Also you might need to "yum install sshpass"
And change /etc/ssh/ssh_config
StrictHostKeyChecking ask
to:
StrictHostKeyChecking no

Python - FileNotFoundError when dealing with DMZ

I created a python script to copy files from a source folder to a destination folder, the script runs fine in my local machine.
However, when I tried to change the source to a path located in a server installed in a DMZ and the destination to a folder in a local servers I got the following error:
FileNotFoundError: [WinError 3] The system cannot find the path specified: '\reports'
And Here is the script:
import sys, os, shutil
import glob
import os.path, time
fob= open(r"C:\Log.txt","a")
dir_src = r"\reports"
dir_dst = r"C:\Dest"
dir_bkp = r"C:\Bkp"
for w in list(set(os.listdir(dir_src)) - set(os.listdir(dir_bkp))):
if w.endswith('.nessus'):
pathname = os.path.join(dir_src, w)
Date_File="%s" %time.ctime(os.path.getmtime(pathname))
print (Date_File)
if os.path.isfile(pathname):
shutil.copy2(pathname, dir_dst)
shutil.copy2(pathname, dir_bkp)
fob.write("File Name: %s" % os.path.basename(pathname))
fob.write(" Last modified Date: %s" % time.ctime(os.path.getmtime(pathname)))
fob.write(" Copied On: %s" % time.strftime("%c"))
fob.write("\n")
fob.close()
os.system("PAUSE")
Okay, we first need to see what kind of remote folder you have.
If your remote folder is shared windows network folder, try mapping it as a network drive: http://windows.microsoft.com/en-us/windows/create-shortcut-map-network-drive#1TC=windows-7
Then you can just use something like Z:\reports to access your files.
If your remote folder is actually a unix server, you could use paramiko to access it and copy files from it:
import paramiko, sys, os, posixpath, re
def copyFilesFromServer(server, user, password, remotedir, localdir, filenameRegex = '*', autoTrust=True):
# Setup ssh connection for checking directory
sshClient = paramiko.SSHClient()
if autoTrust:
sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #No trust issues! (yes this could potentially be abused by someone malicious with access to the internal network)
sshClient.connect(server,user,password)
# Setup sftp connection for copying files
t = paramiko.Transport((server, 22))
t.connect(user, password)
sftpClient = paramiko.SFTPClient.from_transport(t)
fileList = executeCommand(sshclient,'cd {0}; ls | grep {1}'.format(remotedir, filenameRegex)).split('\n')
#TODO: filter out empties!
for filename in fileList:
try:
sftpClient.get(posixpath.join(remotedir, filename), os.path.join(localdir, filename), callback=None) #callback for showing number of bytes transferred so far
except IOError as e:
print 'Failed to download file <{0}> from <{1}> to <{2}>'.format(filename, remotedir, localdir)
If your remote folder is something served with the webdav protocol, I'm just as interested in an answer as you are.
If your remote folder is something else still, please explain. I have not yet found a solution that treats all equally, but I'm very interested in one.

Code to login to remote directory and search a file and download in Python over FTP

I've a package with .gz and filename as (xxxxxxxxxxxx_PARSERxxxxx.tar.gz) extension placed in a remote directory say 1.1.1.1(Should be a input as a variable), i'm currently running the script from machine say 2.2.2.2, i need to place the package in machine 3.3.3.3 (Should be input as avariable), prepared a script for the same but its not working, pls help me out on the same,
Here is the code which i scripted so far(Not working):
#!/usr/bin/python
#ftp.py
import sys
import telnetlib
from ftplib import FTP
import os
def handleDownload(block):
file.write(block)
print ".",
hstip= raw_input('Enter the hstip: ') #ip of the machine where packages ae placed
uid = raw_input('Enter the username: ') #uid os the machine whr packages are placed
pwd = raw_input('Enter the user password: ') #pwd of hte machine where packages are palced
path = raw_input('Enter the path where packages are present: ') #path for the packages.
rmthstip= raw_input('Enter the hstip: ') #ip of the machine where packages to be placed
rmtuid = raw_input('Enter the username: ') #uid os the machine whr packages to be placed
rmtpwd = raw_input('Enter the user password: ') #pwd of hte machine where packages to be palced
cwd = os.getcwd()
os.chdir(cwd)
logout = 'parser files downloaded succesfully'
tn = telnetlib.Telnet(rmtip)
tn.read_until("login: ")
tn.write(rmtuid + "\n")
tn.read_until("Password:")
tn.write(rmtpwd + "\n")
ftp=FTP(hstip,uid,pwd)
print 'Logging in.'
ftp.login(uid,pwd)
ftp.cwd(path)
parserfile = 'find . -name "*PARSER*.gz"'
filenm = os.system(parserfile)
print filenm
ftp.retrbinary(' RETR ', 'filenm', logout )
ftp.quit()
tn.close()
It would really help if you told us how the program is not working.
By glancing quickly over it, I'd say you perserfile bit and filenm usage are wrong.
os.system returns an integer, so filenm is not a file name, but the exist status of the find command. You want to use pipe = subprocess.Popen(['find', '.', '-name', '*PARSER*.gz'], stdout=subprocess.PIPE and a for loop over pipe.stdout to read all the lines produced by the command
the ftp.retrbinary line looks suspicious: you probably want to remove the quotes around 'filenm' to use the path found above, and to use your callback function which needs fixing (see below)
in handleDownload, the call to file.write will fail because you are calling on the file class and not on a file object
Finally, it looks strange to me to perform a search on local file system to find a path on the remote ftp server which looks like what your code is doing, unless you have some specificities to your local path. Or are you trying to use the telnet connection to perform a remote search? IMO, if the FTP server allows you to do so and you know the remote path, you'd better set the CWD on the FTP server to that dir, retrieve the contents and look for a path that matches your requirements.
The downloading per se should be rewritten as:
local_fobj = file('downloaded_file.gz', 'wb')
def download_cb(data):
local_fobj.write(data)
ftp.retrbinary('RETR %s' % filenm, download_cb)
local_fobj.close()
Other misc stuff:
cwd = os.getwd() followed by os.chdir(cwd) can be safely removed
rmtip is not defined, should by rmthstip

Categories