How to copy a file to mapped network drive with python - python

I'm trying to copy files from a folder in my C drive to a mapped network Drive (Z) and am receiving an error using shutil.copy, can someone please tell me where I'm going wrong? Thanks! Here's the code below, much of which is borrowed from another SO post
import os, shutil, re
from os import path
start_file = "C:\\data\\"
end_file = "Z:\\test\\"
files = os.listdir(start_file)
file_list = [i for i in os.listdir(start_file) if and path.isfile(path.join(start_file, i))]
for f in file_list:
shutil.copy(path.join(start_file, f), end_file)
The exact error I'm getting is "Exception has occurred: FileNotFoundError
[Errno 2] No such file or directory" on the last line of that code block

Related

Python error FileNotFoundError: [Errno 2] No such file or directory:

Im trying to run my code with this but keep running into a file not found error.
files = [i for i in os.listdir('C:/Users/me/Desktop/python data')]
for filename in files:
data = pandas.read_excel(str(filename))
I've tried looking around but cant seem to understand.
Running print(os.getcwd()) does find the file in the folder but i still get the error message
You need to concatenate the path and the filename returned from os.listdir:
PATH = 'C:/Users/me/Desktop/python data'
files = [os.path.join(PATH, i) for i in os.listdir(PATH)]
for filename in files:
data = pandas.read_excel(str(filename))
Further recommendations:
You can use pathlib's .glob to get the full path without using os.path.join.
Also, if you use read_excel, please consider filtering by xls/xlsx files:
Code example:
import pathlib
path = pathlib.Path('C:/Users/me/Desktop/python data')
excel_filter = "*.xls*"
for filename in path.glob(excel_filter):
print(filename)

How do I fix a shutil.copy issue?

I keep getting an error that says
"IOError: [Errno 2] No such file or directory: 'C:\Temp\test2_empty\Storage\Poly1.kml'"
What I want to do is copy a file from a directory move it to a temporary storage folder and rename the file and then move that file to another folder. What is the best way to fix this issue?
from qgis.core import*
import glob, os, shutil, time, qgis
path = r"C:\Temp\test2_kml"
dest = r"C:\Temp\test2_empty"
storage = r"C:\Temp\test2_empty\Storage"
for root,d_names,f_names in os.walk(path):
if not f_names:
continue
prefix = os.path.basename(root)
for f in f_names:
if f.endswith('.kml'):
src = os.path.join(root,f)
print("...")
print(time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(src))))
print(os.path.realpath(src))
print(f)
shutil.copy2(src, storage)
for root2,d_names2,f_names2 in os.walk(storage):
for f2 in f_names2:
src2= os.path.join(root2,f2)
os.rename(os.path.join(root2,f2), os.path.join(root2, "{}_{}".format(prefix,f2)))
shutil.move(src2, dest)
Create your destination directory - os.makedirs(storage) before calling shutil copy.
If you want to accept the pre-existence of this directory, you can:
for python 3.2+ - add keyword argument exist_ok=True
for python <3.2 - add try / except block for OSError exception.

Can't resolve error while moving files from one directory to another directory with Python

I have a question that
"Create a program that read the names of files in directory 'Task2' the names are in format UmSn where m=1 to 40 and n=1 to 40 separate the files into different directories based on m like U1,U2,U3......U40."
Hints: use 'os' module for reading directories and filenames.
I tried to solve it but can't.
Here is my code.
import shutil
import os,fnmatch
os.chdir("D:/MCS 2/MCS4/SL/Task2")
for i in range(1,41):
os.mkdir("U"+str(i))
files = os.listdir()
pattern = "*.TXT"
for i in range(1,41):
for f in files:
if f.startswith("U"+str(i)) and fnmatch.fnmatch(f, pattern):
shutil.move(f,("U"+str(i)))
I tried a lot but can't resolve this error.
Traceback (most recent call last):
File "C:\Users\kaleemi\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 557, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'U10S1.TXT' -> 'U10\\U10S1.TXT'
Files start withU1 T0 U9 moves successfully but generate error while moving U10S1.TXT.
Hence the file also U10S1.TXTexist in directory.
Please help me to find where I am doing wrong in my code.
Perhaps you can try making sure you provide the absolute path instead with os.path.abspath():
from os.path import abspath
...
shutil.move(abspath(f),("U"+str(i)))

IOError: [Errno 2] No such file or directory trying to open a file in python

I am new to Python so please forgive the following basic code and problem, but I have been trying to figure out what is causing the error I am getting.
Here is what I am trying to do:
loop through a specific folder
iterate each csv files in that specific folder
perform calculations
Here is my code:
import os
import csv
def get_all_files(directory):
dir_list = os.listdir(directory)
csv_files = []
for e in dir_list:
if e.endswith('.csv'):
csv_files.append(e)
return csv_files
def sum_from_csv(csv_file):
cr = csv.reader(open(csv_file, 'r'))
cr.next()
file_content=cr.readlines()
#initialize throughput total as zero
throughput_total=0
#array to save throughput in every iteration
throughput_dataset=[]
for line in file_content:
line=line.strip()
data=line.split(",")[1]
float_data=float(data)
throughput_total+=float_data
throughput_dataset.append(float_data)
#to calculate number of dataset
dataset_length=len(throughput_dataset)
throughput_average=throughput_total/dataset_length
throughput.append(throughput_average)
print "Throughput-total is",throughput_total
print "Average is",throughput_average
csv_files = get_all_files('/home/gwthamy/Desktop/MyProject/siddhi-benchmarks/filter-4.0.0-M20/filtered-results-filter-4.0.0-M20')
for each in csv_files:
sum_from_csv(each)
Here is the error I am getting:
IOError: [Errno 2] No such file or directory: 'output-0-1502441456439.csv'
I have confirmed that the folder and file do exist. What is causing the IOError and how to I resolve it? Also, is there anything else wrong with my code that would prevent me from performing the entire task?
Thanks in advance
this should work !
import os
dir = '/home/gwthamy/Desktop/MyProject/siddhi-benchmarks/filter-4.0.0-M20/filtered-results-filter-4.0.0-M20'
for each in get_all_files(dir):
sum_from_csv(os.path.join(dir, each))

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