Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python2.7:列出文件夹和子文件夹中的文件并将它们写入文件错误_Python_Python 2.7_File - Fatal编程技术网

Python2.7:列出文件夹和子文件夹中的文件并将它们写入文件错误

Python2.7:列出文件夹和子文件夹中的文件并将它们写入文件错误,python,python-2.7,file,Python,Python 2.7,File,我在Python2.7中编写了这段短代码,它应该将所有文件和子文件夹列成一个字符串。然后将它们的ascii值乘以multi,并将其连接到long\u string。长字符串应该写入一个文件,但它没有发生我不知道为什么。有人能帮我吗? 没有错误消息,只是没有创建文件 长字符串长度为200万个字符 守则: import os from random import randint username = "" for name in ('LOGNAME', 'USER', 'LNAME', 'USERN

我在Python2.7中编写了这段短代码,它应该将所有文件和子文件夹列成一个字符串。然后将它们的ascii值乘以
multi
,并将其连接到
long\u string
。长字符串应该写入一个文件,但它没有发生我不知道为什么。有人能帮我吗? 没有错误消息,只是没有创建文件

长字符串
长度为200万个字符

守则:

import os
from random import randint
username = ""
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
    if os.environ.get(name):
        username = os.environ.get(name)

long_string = ""

n = 10
multi = 12349790

filelist = os.popen("dir C:\users\%s\desktop /s /b"%(username)).read()
for c in filelist:
    number = str(ord(c)*multi)
    while len(number) < n:
        number = "0"+number
    long_string = long_string+number

with open("filelist.txt", "w") as outf:
    outf.write(long_string)

a = [long_string[i:i+n] for i in range(0, len(long_string), n)]

long_string2 = ""

for e in a:
    long_string2 = long_string2+chr(int(e)/multi)

lista_files = long_string2.split("\n")

print lista_files[:5]
导入操作系统
从随机导入randint
username=“”
对于('LOGNAME','USER','LNAME','USERNAME')中的名称:
如果os.environ.get(名称):
用户名=os.environ.get(名称)
long_string=“”
n=10
multi=12349790
filelist=os.popen(“目录C:\users\%s\desktop/s/b”%(用户名)).read()
对于文件列表中的c:
编号=str(ord(c)*多个)
而len(number)
您确定要在正确的目录中查找
“filelist.txt”
?也许它在另一个目录中,你应该在创建文件时写下完整的路径。我还没有考虑过。我现在将尝试更改代码。与您的问题相关的唯一行是
long\u string=“
,其中open(“filelist.txt”,“w”)作为outp:outp.write(long\u string)
。您应该能够删除所有其他内容并重现问题。对吗?还有,你是如何运行脚本的?从何处运行会影响写入文件的默认位置。