I need to move files from my PC to a network location, however if I execute the script I get an error. If have tested this to execute on my PC to a different local folder and it works perfectly.
Here is my code which I got, and modified slightly, from https://thispointer.com/python-how-to-move-files-and-directories/ (giving credit to the author):
import shutil, os, glob, time
def moveAllFilesinDir(srcDir, dstDir):
# Check if both the are directories
if os.path.isdir(srcDir) and os.path.isdir(dstDir) :
# Iterate over all the files in source directory
for filePath in glob.glob(srcDir + '\*'):
# Move each file to destination Directory
if(os.path.getctime(filePath) != os.path.getmtime(filePath)):
shutil.move(filePath, dstDir);
else:
print("srcDir & dstDir should be Directories")
sourceDir = r"C:\Folder A"
destDir = r"\\Server\Folder B"
moveAllFilesinDir(sourceDir,destDir)
Any help will be highly appreciated.
Update
I forgot to mention that I am making use of Remote Desktop to access the server.
Errors I receive:
FileNotFoundError: [WinError 67] The network name cannot be found.
FileNotFoundError: [Errno 2] No such file or directory
Related
I am trying to divide the image dataset into train and test. For this I am copying the images from one folder to other in python. For this I have given the address of both source and destination. But the problem arises when it displays the above error. It can not find the image files to copy. Although I have given correct image address which is "C:\Users\DELL\coil-20-unproc\imagename". Still can't copy the images
original_dataset_dir=r"C:\Users\DELL\coil-20-unproc"
# Copy object1 images to train_obj1_dir
fnames = ['obj1_{}.png'.format(i) for i in range(0,72)]
for fname in fnames:
src = os.path.join(original_dataset_dir, fname)
dst = os.path.join(train_obj1_dir, fname)
shutil.copyfile(src, dst)
Jupyter notebook and Jupyter lab refer to relative path from location that it was started up. You can try these
Copy the file to your startup directory.
(You could enter !pwd in a cell and execute to find put your startup directory)
Create a link from a file in your startup directory to that file.
OS: Windows 10
Python: 3.6 (Anaconda)
I am trying to use a simple temporary file with a context manager to write a simple csv.
import csv
import tempfile
fp = tempfile.TemporaryFile()
with open(fp.name,'w',newline='') as f:
csv_out = csv.writer(f)
csv_out.writerow(['first_name','last_name'])
csv_out.writerow(['foo','bar'])
Running this results in this permission error:
with open(fp.name,'w',newline='') as f:
E PermissionError: [Errno 13] Permission denied: 'C:\\TEMP\\tmp2bqke7f6'
Changing the Windows permission status on the temp directory C:\TEMP\ to allow full access by all users does not help.
Per this post I tried running my Windows cmd as Admin, still did not work.
Searching for a similar problem (link), I found (and tested) a solution which works for your problem as well.
You just need to add a delete=False argument in your fp = tempfile.TemporaryFile() line.
It seems that the file actually gets created in that line, and then trying to open it and write in it a second time (with open(fp.name)...) forbids you do to so.
I have a Python app using Pygraphviz that works fine locally, but on the server the draw function throws an error. It happens in make_svg. The following lines are the relevant part of the errors I get. (The full trail is here.)
File "/path/to/app/utils/make_svg.py", line 17, in make_svg
prog='dot'
File "/path/to/pygraphviz/agraph.py", line 1477, in draw
fh = self._get_fh(path, 'w+b')
File "/path/to/pygraphviz/agraph.py", line 1506, in _get_fh
fh = open(path, mode=mode)
FileNotFoundError: [Errno 2] No such file or directory: 'app/svg_files/nope.svg'
Logging type(g) gives <class 'pygraphviz.agraph.AGraph'> as expected.
I work in a virtualenv in a mod_wsgi 4.6.5/Python3.7 environment on a Webfaction server.
Locally I use a virtualenv with Python 3.5.
The version of Pygraphviz is 1.3.1.(First I had 1.5 on the server. The error was exactly the same, except for the line numbers.)
What can I do?
The same error is described in this bug report from last year.
I don't get which directory I am supposed to create. svg_files exists and has rights 777.
The draw function at the end of make_svg should create the SVG.(And at the end of extract_coordinates_from_svg the file is removed again.)The file name is a hash created in connected_dag (svg_name).
On the server app/svg_files seems not to describe the same place as locally.
I defined the path unambiguously, and now it works.
file_path = '{grandparent}/svg_files/{name}.svg'.format(
grandparent=os.path.dirname(os.path.dirname(__file__)),
name=name
)
g.draw(file_path, prog='dot')
I have variable address = /data/train/1.jpg, and I'm trying to read file by
im = Image.open(address)
FileNotFoundError: [Errno 2] No such file or directory: '/data/train/1.jpg'
By some reasons I can't use full name of file.
I started jupyter notebook from folder which actually contains file 1.jpg in /data/train/.
How can I fix it?
Relative addressing means from the perspective of the current working directory. So if you script is in the same directory that the data folder is in, your path to the file would be ./data/train/1.jpg. Note the ./, which means the current directory.
use relative path, this one is absolute
address = './data/train/1.jpg'
im = Image.open(address)
in this case . means current location while slash means the environments root (view and explanation) on this depends on your OS
I have a main folder called 'test', the inner structure is:
# folders and files in the main folder 'test'
Desktop\test\use_try.py
Desktop\test\cond\__init__.py # empty file.
Desktop\test\cond\tryme.py
Desktop\test\db\
Now in the file tryme.py. I want to generate a file in the folder of 'db'
# content in the file of tryme.py
import os
def main():
cwd = os.getcwd() # the directory of the folder 'Desktop\test\cond'
folder_test = cwd[:-4] # -4 since 'cond' has 4 letters
folder_db = folder_test + 'db/' # the directory of folder 'db'
with open(folder_db + 'db01.txt', 'w') as wfile:
wfile.writelines(['This is a test.'])
if __name__ == '__main__':
main()
If I directly run this file, no problem, the file 'db01.txt' is in the folder of 'db'.
But if I run the file of use_try.py, it will not work.
# content in the file of use_try.py
from cond import tryme
tryme.main()
The error I got refers to the tryme.py file. In the command of 'with open ...'
FileNotFoundError: [Error 2] No such file or directory: 'Desktop\db\db01.txt'
It seems like the code
'os.getcwd()'
only refers to the file that calls the tryme.py file, not the tryme.py file itself.
Do you know how to fix it, so that I can use the file use_try.py to generate the 'db01.txt' in the 'db' folder? I am using Python3
Thanks
Seems like what you need is not the working directory, but the directory of the tryme.py file.
This can be resolved using the __file__ magic:
curdir = os.path.dirname(__file__)
Use absolute filenames from an environment variable, or expect the db/ directory to be a subdirectory of the current working directory.
This behavior is as expected. The current working directory is where you invoke the code from, not where the code is stored.
folder_test = cwd # assume working directory will have the db/ subdir
or
folder_test = os.getEnv('TEST_DIR') # use ${TEST_DIR}/db/