Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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
在Python中访问动态命名目录_Python_Windows_Null - Fatal编程技术网

在Python中访问动态命名目录

在Python中访问动态命名目录,python,windows,null,Python,Windows,Null,我目前正在用Python编写一个脚本,它将执行以下操作:- 在我的Dropbox文件夹中创建一个名为“Spartacus”的目录 使用创建日期和时间的命名约定在此位置创建子目录 在该目录中,创建一个名为iprecord.txt的文件,然后将信息写入该文件 下面是我在Windows 7上使用Python v2.7的代码thusfar:- import os import time import platform import urllib current_dir = os.getcwd()

我目前正在用Python编写一个脚本,它将执行以下操作:-

  • 在我的Dropbox文件夹中创建一个名为“Spartacus”的目录
  • 使用创建日期和时间的命名约定在此位置创建子目录
  • 在该目录中,创建一个名为iprecord.txt的文件,然后将信息写入该文件
下面是我在Windows 7上使用Python v2.7的代码thusfar:-

import os
import time
import platform
import urllib

current_dir = os.getcwd()
targetname = "Spartacus"
target_dir = os.path.join(current_dir, targetname)
timenow = time.strftime("\%d-%b-%Y %H-%M-%S")

def directoryVerification():
    os.chdir(current_dir)
    try:
        os.mkdir('Spartacus')
    except OSError:
        pass
    try:
        os.system('attrib +h Spartacus')
    except OSError:
        pass

def gatherEvidence():
    os.chdir(target_dir)
    try:
        evidential_dir = os.mkdir(target_dir + timenow)
        os.chdir(evidential_dir)
    except OSError:
        pass
    f = iprecord.txt
    with f as open:
        ip_addr = urllib.urlopen('http://www.biranchi.com/ip.php').read()
        f.write("IP Address:\t %s\t %s" % ip_addr, time.strftime("\%d-%b-%Y %H-%M-%S"))


x = directoryVerification()
y = gatherEvidence()
我一直在第26行遇到一个错误,它无法解析动态命名目录(日期和时间)的完整路径。我已经打印出'emplicial_dir'的值,它显示为Null

有没有关于我错在哪里的指示?谢谢

PS:如果您对我的代码有任何其他改进意见,我们将不胜感激
PPS:关于如何找到“Dropbox”的默认目录有什么建议吗?有没有一种方法可以扫描文件系统中名为“Dropbox”的目录并捕获路径?

os.mkdir
不会返回任何内容

evidential_dir = target_dir + timenow
try:
    os.mkdir(evidential_dir)
except OSError:
    pass
os.chdir(evidential_dir)
os.mkdir()。似乎您在代码的不同位置对同一事物使用了不一致的方法。
试试这个:

evidential_dir = os.path.join(target_dir, timenow)
os.mkdir(evidential_dir)
并修复您的另一行:

f = "iprecord.txt"