Having problems with python putting 2 backslashes in path - python-3.x

I have this problem that python puts 2 backlashes in an path and I don't understand why.
Here is the code:
with open(os.path.join("Users", "Kasutaja", "AppData", "Local", "Growtopia", "save.dat"), 'r+') as f:
file_data = f.read()
It gives me this=
C:\Users\Kasutaja\AppData\Local\Programs\Python\Python39\python.exe "C:/Users/Kasutaja/Desktop/Growtopia Hack.py"
Game not found! Make sure to open this before Growtopia
Traceback (most recent call last):
File "C:\Users\Kasutaja\Desktop\Growtopia Hack.py", line 61, in <module>
with open(os.path.join("Users", "Kasutaja", "AppData", "Local", "Growtopia", "save.dat"), 'r+') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'Users\\Kasutaja\\AppData\\Local\\Growtopia\\save.dat'
Process finished with exit code 1

Related

Python3 gzip create new file which did not exist yet

I struggle with python3.9, maybe I'm just blind, so give me a hint please.
Accoring to documententation the following code should create a ".gz"-file.
import gzip
content = b"Lots of content here"
with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
f.write(content)
Is it correct to assume that the file file.txt.gz does not exist before that operation and it will be created during that operation?
My code looks like:
import gzip
...
class FileHandler:
...
def archive(self):
self.content = 'Hello compressed World'
zipFile = '/home/user/archive/test.gz'
print(f'{zipFile}')
with gzip.open(zipFile, 'wt') as f:
f.write(self.content)
...
if __name__ == '__main__':
fp = FileHandler()
fp.archive()
I get the following exception:
Traceback (most recent call last):
File "/home/user/filehandling.py", line 55, in <module>
fp.archive()
File "/home/user/filehandling.py", line 46, in archive
with gzip.open(zipFile, 'wt') as f:
File "/usr/lib/python3.9/gzip.py", line 58, in open
binary_file = GzipFile(filename, gz_mode, compresslevel)
File "/usr/lib/python3.9/gzip.py", line 173, in __init__
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/archive/test.gz'
Obvisiously the file does not exist. Why is it not created as mentioned in the documentation?
So what I am doing wrong here?
---- SOLVED ----
#Czaporka is right, I was missing a part of the path

Python: PermissionError: [Errno 13] with copy_tree function in windows

Im just copying all files and subfolders from an specific directory using the function distutils.dir_util.copy_tree in Windows:
from distutils.dir_util import copy_tree
from datetime import datetime
directories_to_backup = {
'folder_a' : 'e:\\folder_a',
}
now = datetime.now()
backup_name_folder = now.strftime("%Y%m%d-%H%M")
for name_folder, path_folder in directories_to_backup.items():
print(f'Backuping up the folder {name_folder}')
backup_full_path = f't:\\zzzBackup\\{backup_name_folder}\\{name_folder}\\'
copy_tree(path_folder, backup_full_path)
it works fine till it get to some specific file and rise the following error:
Traceback (most recent call last):
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\file_util.py", line 48, in _copy_file_contents
buf = fsrc.read(buffer_size)
PermissionError: [Errno 13] Permission denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/UserR/PycharmProjects/my_scripts/main_backup.py", line 28, in <module>
copy_tree(path_folder, backup_full_path)
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 159, in copy_tree
verbose=verbose, dry_run=dry_run))
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 159, in copy_tree
verbose=verbose, dry_run=dry_run))
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 159, in copy_tree
verbose=verbose, dry_run=dry_run))
[Previous line repeated 1 more times]
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 163, in copy_tree
dry_run=dry_run)
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\file_util.py", line 151, in copy_file
_copy_file_contents(src, dst)
File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\file_util.py", line 51, in _copy_file_contents
"could not read from '%s': %s" % (src, e.strerror))
distutils.errors.DistutilsFileError: could not read from 'e:\folder_a\0\binlog': Permission denied
Process finished with exit code 1
How can I update the permissions in windows so my script would be able to read and copy the files?

file not found error, though file is in proper place

C:\Users\ebena\PycharmProjects\untitled2\venv\Scripts\python.exe C:/Users/ebena/PycharmProjects/untitled2/trial.py
Traceback (most recent call last):
File "C:/Users/ebena/PycharmProjects/untitled2/trial.py", line 5, in <module>
image = Image.open("book background.jpeg")
File "C:\Users\ebena\PycharmProjects\untitled2\venv\lib\site-packages\PIL\Image.py", line 2878, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'book background.jpeg'
python cannot access these files and says they do not exist or file not found
Change the name of the file to book_background.jpeg

FileNotFoundError: [Errno 2] No such file or directory: '2MCREF~E.JPG'

I'm trying to open an image that resides at a different location than my script
Code:
import os
from PIL import Image
folder = '/Users/abc'
if not os.listdir(folder):
print('Folder not found')
else:
print('"{}" found'.format(folder))
for file in os.listdir(folder):
print(file)
data = Image.open(file,'r')
print('Done')
Error:
"/Users/abc" found
2MCREF~E.JPG
Traceback (most recent call last):
File "img_to_s3bucket.py", line 25, in <module>
data = Image.open(file,'r')
File "/Users/AjayB/anaconda3/envs/MyDjangoEnv/lib/python3.6/site-packages/PIL/Image.py", line 2770, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '2MCREF~E.JPG'
How to tackle this?
This could be because your working directory and the location of the file are not same.
You could do this by specifying the full file path in the below command:
data = Image.open(file,'r')
you can do this by:
data = Image.open(os.path.join(folder, file),'r'))

how to import .txt with space in filename

Code:
import sys
with open(sys.argv[1]) as f:
lines = f.readlines()
for line in lines[0:]:
columns = line.split()
print(columns[0])
Output:
Traceback (most recent call last):
File "scissors.py", line 2, in <module>
with open (sys.argv[1]) as f:
FileNotFoundError: [Errno 2] No such file or directory: '1'
(Links to old images: https://i.stack.imgur.com/aG1vr.png, https://i.stack.imgur.com/wSpLG.png)
Wrap the file name in quotation marks like this "your file.txt"
If you have a filepath, use the raw string (and backslashes in the copied filepath) by placing a r before the quotation marks:
r"C:\Folder\Subfolder\another_one\your_file.txt"

Resources