In linux system using python.I read through Python - Read Files based on Absolute Path their solution is switch to 'F:', so is different from mine.
usb file path /home/joy/aaa.txt
is what I can get, but file read/print only accept ,/media/joy/Data/aaa.txt
path, (these 2 path point to same text file)
Question1:
I cannot generate /media/joy/Data/aaa.txt
path myself, so that I can't read/write
Question2:
or there is other way to read/write usb file
from pathlib import Path
import time
from usb.core import find
from usb.util import get_string
import usb.backend.libusb1
import re
import usb.core
import usb.util
import os
import numpy as np
import os.path
from os import path
print("File Path:", Path("aaa.txt").absolute())
print("Directory Path:", Path().absolute())
#with open('/home/joy/aaa.txt') as f:
# contents_path1 = f.read()
#print(contents_path1)
with open('/media/joy/Data/aaa.txt') as f:
contents_path2 = f.read()
print(contents_path2)
the output with /media/joy/Data/aaa.txt
(can read/write , but I don't know how to get this path, another path just not work)-
File Path: /home/joy/aaa.txt
Directory Path: /home/joy
The input:
5555555
66666666666
6
665
13
6
654521
tried code: tried to use os.path.abspath("relative path")
, but even is work, we just manual change /home/joy/aaa.txt
to /media/joy/Data/aaa.txt
without the function can get relative path, in other USB plug in is not work again, so....
from pathlib import Path
import time
from usb.core import find
from usb.util import get_string
import usb.backend.libusb1
import re
import usb.core
import usb.util
import os
import numpy as np
import os.path
from os import path
#with open('/home/joy/aaa.txt') as f:
# contents_path1 = f.read()
#print(contents_path1)
with open('/media/joy/Data/aaa.txt') as f:
contents_path2 = f.read()
print(contents_path2)
dirname = os.path.dirname("aaa.txt")
filename = os.path.join(dirname, '/dadasad/aaa.txt')
print(filename)
print("File Path:", Path("aaa.txt").absolute())
print("Directory Path:", Path().absolute())
and the output is :
33333
55555
444
/dadasad/aaa.txt
File Path: /home/joy/aaa.txt
Directory Path: /home/joy
Try to add the mode of opening the file like:
# for reading
with open('/media/joy/Data/aaa.txt', 'r') as f:
contents_path2 = f.read()
print(contents_path2)
with open('/media/joy/Data/aaa.txt', 'w') as f:
f.write(data)