I have a function where I am trying to copy an XML document to my Dropbox folder, but I am getting:
/usr/bin/python3.5 /home/cali/PycharmProjects/Vocabulary/Vocabulary.py
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
return self.func(*args)
File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", line 232, in add_item
self.sync()
File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", line 287, in sync
copyfile(vocabulary_path, destination_path)
File "/usr/lib/python3.5/shutil.py", line 115, in copyfile
with open(dst, 'wb') as fdst:
IsADirectoryError: [Errno 21] Is a directory: '/home/cali/Dropbox/'
Process finished with exit code 0
Here is the function:
def sync(self):
path = os.path.expanduser('~/Desktop')
vocabulary_path = os.path.join(path, 'Vocabulary', 'Words.xml')
destination_path = os.path.expanduser('~/Dropbox/')
copyfile(vocabulary_path, destination_path)
How can I overcome the issue?
Perhaps by copying the file to another file, rather than a directory.
...
destination_path = os.path.expanduser('~/Dropbox/Words.xml')
...
Related
Hello I try to do buildozer init but i got this message
Traceback (most recent call last):
File "/home/vanthao/.local/bin/buildozer", line 8, in <module>
sys.exit(main())
File "/home/vanthao/.local/lib/python3.8/site-packages/buildozer/scripts/client.py", line 13, in main
Buildozer().run_command(sys.argv[1:])
File "/home/vanthao/.local/lib/python3.8/site-packages/buildozer/__init__.py", line 1010, in run_command
getattr(self, cmd)(*args)
File "/home/vanthao/.local/lib/python3.8/site-packages/buildozer/__init__.py", line 1049, in cmd_init
copyfile(join(dirname(__file__), 'default.spec'), 'buildozer.spec')
File "/usr/lib/python3.8/shutil.py", line 264, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
PermissionError: [Errno 13] Permission denied: 'buildozer.spec'
Could anyone explain what problem I am facing and how to resolve it? Thank you!
It can meen, your variable src isn't correct.
If
src = "C:\User\name\folder_exemple"
Add a "r" before the path value:
src = r"C:\User\name\folder_exemple"
I have this problem, I'm trying to run the script to download Springers free books [https://towardsdatascience.com/springer-has-released-65-machine-learning-and-data-books-for-free-961f8181f189], but many things start to go wrong.
I solved some of the problems but now I'm stuck.
C:\Windows\system32>python C:\Users\loren\Desktop\springer_free_books-master\main.py
Traceback (most recent call last):
File "C:\Users\loren\Desktop\springer_free_books-master\main.py", line 42, in <module>
books.to_excel(table_path)
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py", line 2175, in to_excel
formatter.write(
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\formats\excel.py", line 738, in write
writer.save()
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_openpyxl.py", line 43, in save
return self.book.save(self.path)
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
save_workbook(self, filename)
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\writer\excel.py", line 291, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Users\loren\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1251, in __init__
self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'downloads\\table_v4.xlsx'
This is part of the code, were table_path is introduced.
table_url = 'https://resource-cms.springernature.com/springer-cms/rest/v1/content/17858272/data/v4'
table = 'table_' + table_url.split('/')[-1] + '.xlsx'
table_path = os.path.join(folder, table)
if not os.path.exists(table_path):
books = pd.read_excel(table_url)
# Save table
books.to_excel(table_path)
else:
books = pd.read_excel(table_path, index_col=0, header=0)
Try to create the destination directory before calling .to_excel() to ensure a valid writable directory exists. Make sure the os module is imported:
import os # add to your imports
and replace
books.to_excel(table_path)
with
os.makedirs(folder, exist_ok=True)
books.to_excel(table_path)
I have a file in the media/ directory:
When I in a test.py file to get it by GeoIP():
from pygeoip import GeoIP
def getLatLon(ip):
gi = GeoIP('media/GeoLite2-City.mmdb')
data = gi.record_by_addr(ip)
return data
getLatLon("113.210.32.76")
There comes issue:
File "/Users/luowensheng/Desktop/TestIOS/TestPython/testDemo02/ipaddr/tests.py", line 16, in <module>
getLatLon("103.200.32.76")
File "/Users/luowensheng/Desktop/TestIOS/TestPython/testDemo02/ipaddr/tests.py", line 10, in getLatLon
gi = GeoIP('media/GeoLite2-City.mmdb')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pygeoip/__init__.py", line 118, in __init__
self._fp = codecs.open(filename, 'rb', ENCODING)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/codecs.py", line 895, in open
file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: 'media/GeoLite2-City.mmdb'
It says the FileNotFoundError. why there comes this error?
I am trying to write a program that sorts my downloads; however, when I try to run it I get this:
Traceback (most recent call last):
File "/usr/lib/python3.5/shutil.py", line 538, in move
os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory:'UntitledDocument.txt' -> 'Downloads/TxtFiles/UntitledDocument.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/nicholas/Python/DownloadSorter.py", line 19, in <module>
shutil.move(file, folders[fileType])
File "/usr/lib/python3.5/shutil.py", line 552, in move
copy_function(src, real_dst)
File "/usr/lib/python3.5/shutil.py", line 251, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/lib/python3.5/shutil.py", line 114, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'UntitledDocument.txt'
This is the code :
#!/usr/bin/python3
import os
import shutil
folders = {
'.tar.gz': 'Downloads/TarFiles',
'.deb': 'Downloads/DebFiles',
'.iso': 'Downloads/IsoFiles',
'.txt': 'Downloads/TxtFiles',
'.exe': 'Downloads/ExeFiles',
'.mp3': 'Videos',
'.wav': 'Music'
}
os.chdir('/home/nicholas/')
for file in os.listdir('Downloads'):
for fileType in folders.keys():
if file.endswith(fileType):
shutil.move(file, folders[fileType])
The problem is that you are using the filename without the path and shutil looks for the file in the working directory intead of Downloads directory.
The fix is simple:
shutil.move(os.path.join('Downloads', file), folders[fileType])
In Python I am trying to copy a directory (actually, its the Jenkins jobs directory), but it contains symbolic links in it, when I copy I get:
Traceback (most recent call last):
File "BackupJenkinsJobs.py", line 272, in <module>
main()
File "BackupJenkinsJobs.py", line 208, in main
distutils.dir_util.copy_tree(JenkinsJobSrc, cleanJobsDir, preserve_symlinks=False)
File "c:\Python27\lib\distutils\dir_util.py", line 163, in copy_tree
verbose=verbose, dry_run=dry_run))
File "c:\Python27\lib\distutils\dir_util.py", line 163, in copy_tree
verbose=verbose, dry_run=dry_run))
File "c:\Python27\lib\distutils\dir_util.py", line 167, in copy_tree
dry_run=dry_run)
File "c:\Python27\lib\distutils\file_util.py", line 148, in copy_file
_copy_file_contents(src, dst)
File "c:\Python27\lib\distutils\file_util.py", line 32, in _copy_file_contents
fsrc = open(src, 'rb')
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\Program Files (x86)\\Jenkins\\jobs\\AutoRunTemplate\\builds\\lastFailedBuild'
I am using the following code:
try:
distutils.dir_util.copy_tree(JenkinsJobSrc, cleanJobsDir, preserve_symlinks=False)
except distutils.errors.DistutilsFileError as e:
print("Unable to copy Jenkins jobs. Error: %s".format(e))
return
Any assistance would be appreciated on how to copy, ignoring the links, as preserve_symlinks doesn't appear to work.