Saving Files to SharePoint 2007 in Python - python

Relatively new Python user here, so please bear with me. As part of a program I am working on, I need to have Python loop through folders in a SharePoint site, copy a file in each folder, and paste the copies into a local folder. I am attempting to use shutil.copy, but I'm getting an error ("OSError: [Errno 22] Invalid argument"). I do not get any errors if I try to copy files from a local drive, leading me to believe SharePoint is the issue.
After doing some research, it appears that I need to install some type of module to work with SharePoint. Is this accurate? Even if I normally just use Windows Explorer to navigate/edit files on the SharePoint site?
The other issue is that my work still uses SharePoint 2007, so I think that may limit my options.
Any help is much appreciated. Thanks!
import xlrd
import shutil
import os
#This is the location of the file on the SharePoint
sp_file = "https://...test.xlsx"
#This is the desired destination locally.
sp_comp_file = "C:\\Users\\555555\\Desktop\\Python Test\\test.xlsx"
shutil.copy(sp_file, sp_comp_file)
Error:
Traceback (most recent call last):
File "C:\Users\555555\Desktop\Python\teststack.py", line 9, in <module>
shutil.copy(sp_file, sp_comp_file)
File "C:\Users\555555\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 235, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\555555\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 114, in copyfile
with open(src, 'rb') as fsrc:
OSError: [Errno 22] Invalid argument: 'https://...test.xlsx'

It might be late now, but try to remove the "https:" from your variable called sp_file such as:
sp_file = "//...test.xlsx"
I'm doing the same kind of copy/paste on a SP site and without the "https:" it works for me.
Not sure it will work, because I don't get the same error than you if I add "https:" in my program.

Related

Errno2, file not found (even though i put the file in the same folder as the script)

I am a beginner trying to make a name sniper, and I have a file i want to open. I keep on getting the file not found error even though i have put it in the same folder. this is the code:
accounts = open("accounts.txt", "r")
accounts = f.read()
it comes back with this error:
File "c:/users/(my username)/AXESNIPE/lib/AXESNIPE.py", line 73, in <module>
accounts = open("accounts.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'accounts.txt'
and this is where my file is:
if anyone can help that would be really cool idk why this is happening ive spent way to long on this ok thanks bye
what is f? you mean accounts.read() i guess
You didn't add full file path i suppose. if your file is where your script is then you may have to add full path of the file or run in inside sep env.
try this :
import os
file = "c:/users/(my username)/AXESNIPE/lib/accounts.txt" # your file path here.
if os.path.exists(file):
accounts = open(file, "r")
accounts = accounts.read()
else:
print(f"No Files Found Matching This Name :: {file}")

Chron - [Permission Denied] when I try to save & remove file from directory

Issue: Unable to save file in directory (/root/Notion/Image) when using Cron schedule
This is what my code is trying to do:
Check email
Download image attachment
Store in a directory - root/Notion/Image
Retrieve file path
The script is working when I run it manually in Google Cloud terminal. The problem is when I try to schedule it on Cron, it's unable to access the folder to save the file locally.
This is the error when the script failed and require permission:
Traceback (most recent call last):
File "Notion/test.py", line 121, in <module>
path = get_attachments(email.message_from_bytes(msg[0][1]))
File "Notion/test.py", line 47, in get_attachments
with open(filePath, 'wb') as f:
PermissionError: [Errno 13] Permission denied: '/root/Notion/Image/3.jpeg'
This is the code to retrieve attachment from email
def get_attachments(msg):
for part in msg.walk():
if part.get_content_maintype()=='multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join(attachment_dir, fileName)
with open(filePath, 'wb') as f:
f.write(part.get_payload(decode=True))
return str(filePath)
Resolved:
The problem is that I shouldn't use root directory since it requires permission. I've changed it to home directory instead.
attachment_dir = '/home/dev_thomas_yang/folder_name/folder_name'
For people who needs to check their home direction, simply run this script.
from pathlib import Path
home= str(Path.home())
print(home)
Thanks Triplee for the patience to breakdown my issue despite my sloppy ways of presenting it!
The easiest fix hands down is to change the code so it doesn't try to write to /root. Have it write to the invoking user's home directory instead.
Your question doesn't show the relevant parts of the code, but just change attachment_dir so it's not an absolute path. Maybe separately take care of creating the directory if it doesn't already exist.
import pathlib
# ...
attachment_dir = pathlib.Path("cron/whatever/attachments").mkdir(parents=True, exist_ok=True)
# ...
for loop in circumstances:
get_attachments(something)
A better design altogether would be to have get_attachments accept the directory name as a parameter, so you can make this configurable from the code which calls it. Global variables are a nuisance and cause hard-to-debug problems because they hide information which is important for understanding the code, and tricky to change when you try to debug that code and don't know which parts of the code depend on the old value.

Move file in Python with shutil that has just been changed

I wrote a Python script which calls subprocess which will create a file:
subprocess.run(["youtube-dl.exe", "-o " + fileName, link])
After that I'd like to move my file to a different directory:
shutil.copy("C:\\Users\\***\\Desktop\\m2.m4a", "C:\\Users\\***\\Desktop\\Songs\\m2.m4a")
But that gives me the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\***\Desktop\m2.m4a'
But the file definitely exists. I tried to move a file which has not just been altered, and it works.
In case Windows needs time to create the file properly, I paused the code after the file creation and before the shutil.move-command:
time.sleep(4)
But it still doesn't work. Is it maybe because subprocess is still running and shutil can't access the file at the same time? Or because Python doesn't know that a new file has just been created? Thanks for your help!

open function returns 404 error on python

I am working on a small python project and found myself having to read a json file. I tried with this little script found on the web, but it gives me a 404 error.
I have a folder containing the json file (datasets.json) and the python file which, for some reason, does not find the json one.
with open('datasets.json', 'r') as file:
dataset = json.loads(file.read())
print(dataset)
Traceback (most recent call last): File "Desktop/proj/ai/index.py", line 4, in with open('datasets.json', 'r') as file: FileNotFoundError: [Errno 2] No such file or directory: 'datasets.json'
The problem is that a relative path depends of the current directory, when you compile a python file the current directory isn't the file's one. Try using an absolute path. You can also transform a relative path to absolute by using os module.
import os
relativePath = './hello/world.py'
absolutePath = os.path.abspath(relativePath)
print(absolutePath)

Shutil.copy IO Error2 when directory exists

I'm encountering a troublesome problem with my code and I've been unable to figure it out. Basically I am copying files from a local directory on my computer to a Dropbox folder that acts as a project repository for me and some other folks.
I keep hitting an IO Error when executing the shutil.copy line. Errno 2, N osuch file or directory. However the directory and file both exist. When I change the directory to a different/test location (test_dir in my code), the code runs perfectly fine. Any insights would be greatly appreciated.
import os, os.path
import re
import shutil
import sys
#File Location
directory_list = "path where files are located"
#Dropbox base directory:
dropbox = "path to dropbox directory"
test_dir = "path to test directory on my local machine"
sed_files = os.listdir(directory_list)
for i in sed_files:
#print i.split("BBB")[0]
#df
copy_dir = re.sub(r'XXX',r'_',i.split("BBB")[0])
copy_dir = re.sub(r'ZZZ',r'/',copy_dir)
copy_dir = dropbox + copy_dir + "/FIXED/"
if not os.path.exists(copy_dir):
os.makedirs(copy_dir)
shutil.copy(directory_list+i,copy_dir)
#print directory_list+i
#os.rename(copy_dir+i,copy_dir+i.split("BBB")[1])
Traceback error is:
Traceback (most recent call last):
File "copy_SE_files.py", line 25, in <module> shutil.copy(direcotry_list+i,copydir)
File "C:\Python27\ArcGIS10.1\lib\shutil.py", line 116, in copy copyfile(src,dst)
File "C:\Python27\ArcGIS10.1\lib\shutil.py", line 82, in copyfile with open(dst, 'wb') as fdst:
IOError: [Errno 2] No such file or directory: 'C:/Users/myusername/Dropbox/NASA_HyspIRI_Project/Field_Data/Spectra/CVARS/April2014/LemonTrees/04172014_SE_LemonTreeCanopy/SE_Files/SpectraZZZCVARSZZZApril2014ZZZLemonTreesZZZZ04172014XXXSEXXXLemonTreeCanopyZZZSEXXXFilesBBBCVARS_na_LemonTrees_Bareground1_4deg_Refl_00355.sed'
Problem solved thanks to the keen eyes of stack overflow. Modified the line to read:
shutil.copy(directory_list+i,'\\\\?\\'+os.path.abspath(copy_dir))
You're failing because the combined length of the path is greater than Window's MAX_PATH limit. C:/Users/myusername/Dropbox/NASA_HyspIRI_Project/Field_Data/Spectra/CVARS/April2014/LemonTrees/04172014_SE_LemonTreeCanopy/SE_Files/SpectraZZZCVARSZZZApril2014ZZZLemonTreesZZZZ04172014XXXSEXXXLemonTreeCanopyZZZSEXXXFilesBBBCVARS_na_LemonTrees_Bareground1_4deg_Refl_00355.sed is 274 characters long, and without going to some trouble, most Windows file manipulation APIs won't work properly with a path longer than MAX_PATH (which is 260, and one of them is reserved for the NUL terminator).
Assuming Python uses the correct APIs, you can make it work with the extended path prefix, \\?\ (and it may require you to use backslashes rather than forward slashes in your path; I'm not clear on that; read the docs).
The first thing that jumped out at me was this line:
shutil.copy(directory_list+i,copy_dir)
Consider changing it to
shutil.copy(os.path.join(directory_list,i),copy_dir)
IOW, use os.path.join() when concatenating file paths.
One work around is:
try:
shutil.copy(src, dest)
except:
try:
shutil.copy(src, "\\\\?\\" + dest)
#If Long Path as per Maximum Path limitation Windows
except:
self.failed_TC=True
print("Failed to move the script "+os.path.basename(src)+" to "+dest)

Categories

Resources