我有一本字典:
my_dictionary = {"058498":"table", "064165":"pen", "055123":"pencil"}
Run Code Online (Sandbox Code Playgroud)
我迭代它:
for item in my_dictionary:
PDF = r'C:\Users\user\Desktop\File_%s.pdf' %item
doIt(PDF)
def doIt(PDF):
part = MIMEBase('application', "octet-stream")
part.set_payload( open(PDF,"rb").read() )
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
IOError: [Errno 2] No such file or directory: 'C:\\Users\\user\\Desktop\\File_055123.pdf'
Run Code Online (Sandbox Code Playgroud)
它无法找到我的文件.为什么它认为文件路径中有双反斜杠?
我不知道为什么,但出于某种原因,每当我在输出文件的文件名中有"region"时,它就会给我这个错误:
IOError:[Errno 22]无效模式('w')或文件名:'path\regionlog.txt'
它为"region.txt","logregion.txt"等执行此操作.
class writeTo:
def __init__(self, stdout, name):
self.stdout = stdout
self.log = file(name, 'w') #here is where it says the error occurs
output = os.path.abspath('path\regionlog.txt')
writer = writeTo(sys.stdout, output) #and here too
Run Code Online (Sandbox Code Playgroud)
为什么是这样?我真的想将我的文件命名为"regionlog.txt",但它不断出现这个错误.有办法解决吗?
redpath = os.path.realpath('.')
thispath = os.path.realpath(redpath)
fspec = glob.glob(redpath+'/*fits')
thispath = os.path.realpath(thispath+'/../../../..')
p = Path(thispath)
userinput = 'n'
while (userinput == 'n'):
text_file = next(p.glob('**/*.fits'))
print("Is this the correct file path?")
print(text_file)
userinput = input("y or n")
parent_dir = text_file.parent.resolve()
fspec = glob.glob(parent_dir+'/*fits')
Run Code Online (Sandbox Code Playgroud)
我收到错误
unsupported operand type(s) for +: 'WindowsPath' and 'str'
Run Code Online (Sandbox Code Playgroud)
我认为这是因为当我需要 glob 一个字符串时,我试图 glob 一个 Windows 文件路径。有没有一种方法可以将 WindowsPath 转换为字符串,以便我可以将所有文件合并到一个列表中?
我在python脚本中使用目录路径面临一个非常基本的问题.当我从Windows资源管理器复制路径时,它使用反斜杠作为路径分隔符导致问题.
>>> x
'D:\testfolder'
>>> print x
D: estfolder
>>> print os.path.normpath(x)
D: estfolder
>>> print os.path.abspath(x)
D:\ estfolder
>>> print x.replace('\\','/')
D: estfolder
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题.
我正在尝试从图像中识别文本,然后输出文本;然而,这个错误吐出来了:
回溯(最近一次通话):文件“C:/Users/Benji's Beast/AppData/Local/Programs/Python/Python37-32/imageDet.py”,第 41 行,打印中(get_string(src_path + "cont.jpg") ) ) 文件“C:/Users/Benji's Beast/AppData/Local/Programs/Python/Python37-32/imageDet.py”,第 15 行,在 get_string img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv:: cvt颜色'
图像分辨率为 1371x51。我曾尝试将 src_path 上的“/”更改为“\”,但这没有用。有任何想法吗?
这是我的代码:
import cv2
import numpy as np
import pytesseract
from PIL import Image
from pytesseract import image_to_string
# Path of working folder on Disk
src_path = "C:/Users/Benji's Beast/Desktop/image.PNG"
def get_string(img_path):
# Read image with opencv
img = cv2.imread(img_path)
# Convert to gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# …Run Code Online (Sandbox Code Playgroud) 因此,每当我尝试使用 Chrome 设置(我在默认浏览器中使用的设置)时,添加
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\... (my webdriver path)")
driver = webdriver.Chrome(executable_path="myPath", options=options)
Run Code Online (Sandbox Code Playgroud)
它向我展示了错误代码
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes n 16-17: truncated \UXXXXXXXX escape
Run Code Online (Sandbox Code Playgroud)
在我的 bash 中。我不知道这意味着什么,如果我能得到任何帮助,我会很高兴。提前致谢!
python selenium google-chrome selenium-chromedriver chrome-profile
我在桌面上创建了一个名为"headfirstpython"的文件夹,我需要将当前的工作目录更改为该文件夹及其内部的子文件夹.我使用os.getcwd()获取当前文件夹,它给了我'C\Python32'.我使用os.chdir('../ headfirstpython/chapter3')来更改目录,但它告诉它无法找到路径
>>> import os
>>> os.getcwd()
'C:\\Python32'
>>> os.chdir('../headfirstpython/chapter 3')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
os.chdir('../headfirstpython/chapter 3')
WindowsError: [Error 3] The system cannot find the path specified: '../headfirstpython/chapter 3'
>>> os.chdir('../headfirstpython/chapter3')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
os.chdir('../headfirstpython/chapter3')
WindowsError: [Error 3] The system cannot find the path specified: '../headfirstpython/chapter3'
>>>
Run Code Online (Sandbox Code Playgroud) 我正在编写一个脚本来解析多个日志文件并维护已处理的文件列表.当我读取要处理的文件列表时,我使用os.walk并获得类似于以下内容的名称:
C:/Users/Python/Documents/Logs\ServerUI04\SystemOut_13.01.01_20.22.25.log
Run Code Online (Sandbox Code Playgroud)
这是由以下代码创建的:
filesToProcess.extend(os.path.join(root, filename) for filename in filenames if logFilePatternMatch.match(filename))
Run Code Online (Sandbox Code Playgroud)
似乎"root"使用正斜杠作为分隔符(我在Windows上找到更方便),但"filename"使用反斜杠,所以我最终得到一个不一致的文件路径,因为它包含前向和后斜杠的混合作为分隔符.
我试过设置分隔符:
os.path.sep = "/"
Run Code Online (Sandbox Code Playgroud)
和
os.sep = "/"
Run Code Online (Sandbox Code Playgroud)
在.join之前,它似乎没有任何效果.我意识到理论上我可以操作字符串,但从长远来看,我希望我的脚本可以在Unix和Windows上运行,所以如果可能的话,它会更喜欢它是动态的.
我错过了什么吗?
更新:
基于下面的有用响应,看起来我的问题是自己造成的,为方便起见,我设置了以root身份使用的初始路径,如下所示:
logFileFolder = ['C:/Users/Python/Documents/Logs']
Run Code Online (Sandbox Code Playgroud)
当我把它更改为:
logFileFolder = ['C:\\Users\\Python\\Documents\\Logs']
Run Code Online (Sandbox Code Playgroud)
一切正常,我生成的文件路径全部使用"\".看起来我的方法是错误的,因为我试图让Python改变行为而不是纠正我设置的值.
谢谢!
我想在python中使用opencv创建一个图像文件.当我在相同的文件夹文件中创建它时创建
face_file_name = "te.jpg"
cv2.imwrite(face_file_name, image)
Run Code Online (Sandbox Code Playgroud)
但是当我试图在另一个文件夹中创建它时
face_file_name = "test\te.jpg"
cv2.imwrite(face_file_name, image)
Run Code Online (Sandbox Code Playgroud)
文件未创建.谁有人解释原因?
我甚至试过给出绝对的路径.我在Windows中使用python2.7.
我有以下代码
pathToFile = "R:\T2 Output\12345--01--Some File 1--ABCD.mp4"
process = subprocess.Popen(['ffprobe.exe', '-show_streams', '"'+pathToFile+'"'],
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
[Error 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
注意事项:
我确定我错过了一些简单的事情.谁能指出我正确的方向?我已经在这个网站和其他人上做了很多搜索并尝试了一些建议.