繁体   English   中英

如何在临时文件中使用FOR创建循环?

[英]How to create a loop with FOR in a temporary file?

我正在使用加密的文件,但是在关闭和删除之前,我无法通过for创建一个循环来读取它。

我的意图是读取加密文件中给出的数据,并将其循环以将每一行分配给一个变量。

每当我执行我的代码时,Python都会直接完成,而无需使用解密的信息。 我相信这是因为with命令在循环开始之前将其关闭。

这就是我想要的,不起作用,也没有错误:

with open(input_file, 'rb') as fp:
  data = fp.read()

fernet = Fernet(key)
encrypted = fernet.decrypt(data)
with tempfile.TemporaryFile() as fp:
  fp.write(encrypted)
  for url in fp: #Python ignores the tempfile. I belive it is closed in the previous line.
    segment = url.strip()
    url = 'https://docs.python.org/3.3/tutorial/' + segment
    filename = segment + '.html'
    filePath = pjoin('Data/' + filename)
    response = urlopen(url)
    webContent = response.read()
    html_content = urlopen(url).read()
    matches = re.findall(b'string', html_content);

    if len(matches) == 0: 
      print(segment + ' unchanged.')

    else:  
      with open(filePath, 'wb') as w:
       w.write(webContent)

这是有效的代码(对不起,试图使其更短但不能):

with open(input_file, 'rb') as fp:
  data = fp.read()

fernet = Fernet(key)
encrypted = fernet.decrypt(data)

with open(output_file, 'wb') as fp:
    fp.write(encrypted)

with open(output_file) as fp:
    for url in fp:
      segment = url.strip()
      url = 'https://docs.python.org/3.3/tutorial/' + segment
      filename = segment + '.html'
      filePath = pjoin('Data/' + filename)
      response = urlopen(url)
      webContent = response.read()
      html_content = urlopen(url).read()
      matches = re.findall(b'string', html_content);

    if len(matches) == 0: 
      print(segment + ' unchanged.')

    else:  
      with open(filePath, 'wb') as w:
       w.write(webContent) 

这两个示例的标题(为了简短起见):

#python 3.6.6

from urllib.request import urlopen
import urllib.request
from os.path import join as pjoin
import re, os, sys, tempfile, six, ctypes, time, fileinput
from cryptography.fernet import Fernet

print("[*] Checking list.dat for consistency . . .")
key = b'wTmVBRLytAmlfkctCuEf59K0LDCXa3sGas3kPg3r4fs=' #Decrypt list.dat
input_file = 'List.dat'
output_file = 'List.txt'

List.txt内容:

errors
classes
stdlib

有什么提示吗?

问题是一旦写入文件,“文件指针”就位于文件的末尾 没有什么可阅读的。

您可以使用seek方法在开头重新定位文件指针。 或者,关闭并重新打开文件(如在您的工作代码中一样)会将指针定位在文件的开头。

@LarryLustig几乎回答了为什么您的代码无法正常工作的问题,但是IMO如果您完全删除了temp文件(这不是必需的),您甚至不必担心游标。 请参阅以下对所需代码的注释更改。

# We'll use os.linesep to get the line terminator string for your os.
import os

...

with open(input_file, 'rb') as fp:
  data = fp.read()

fernet = Fernet(key)

# decode your decrypted bytes into strings.  Change 'utf-8' into whichever file encoding you're using if necessary.
decrypted = fernet.decrypt(data).decode('utf-8')

# Don't write to a temp file
# Iterate directly on each line of the extracted data
for url in decrypted.split(os.linesep): 
    segment = url.strip()
    url = 'https://docs.python.org/3.3/tutorial/' + segment
    filename = segment + '.html'
    filePath = pjoin('Data/' + filename)
    response = urlopen(url)
    webContent = response.read()
    html_content = urlopen(url).read()
    matches = re.findall(b'string', html_content);

    if len(matches) == 0: 
      print(segment + ' unchanged.')

    else:  
      with open(filePath, 'wb') as w:
       w.write(webContent)

另外,如果您确定文件中使用的行终止符是什么(例如\\r\\n\\n ),则可以完全取消使用os.linesep

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM