简体   繁体   English

即使使用绝对路径,Python脚本也无法使用os.path.exists工具找到路径

[英]Python script can't find path using os.path.exists tool even though the absolute path is being used

I'm trying to get this code to take files, rename them randomly, and save a key connecting the random names to the original names. 我正在尝试获取此代码来获取文件,随机重命名并保存将随机名称连接到原始名称的密钥。 At first the code was taking all of the files (as indicated by the counter) and executing the script on only about 2/3 of them. 最初,代码将获取所有文件(如计数器所示),并仅对其中的2/3执行脚本。 Now, it is not even finding my directory even though I am using the absolute path. 现在,即使我使用绝对路径,它甚至都找不到我的目录。 Working on Mac Mojave and python3.7. 在Mac Mojave和python3.7上工作。

I have tried using the relative as well as the absolute path and am executing the script in the terminal when I am in the directory which contains both the script and the directory I want to execute it on. 我尝试使用相对路径和绝对路径,并且在包含脚本和要在其上执行脚本的目录的目录中执行脚本。

#!/usr/bin/env python3

#This program takes files in a directory and renames them as a random number, this random number can be connected
#to the original file as the pairs are stored in a dictionary named key, can be used for blind analysis

import os
import random
import json
import shutil


path = "/Users/krisshirley108/pcfb/bookscripts/8.1schmoocopy/"

if os.path.exists('path'):


  Dir = os.listdir('path') 

  Key = {}

  counter = 0

  for File in Dir:
    print (File) 

    counter = counter + 1

  print (counter)

else:
  print ("failed")

It prints 'failed'. 它打印“失败”。

edit I removed the apostrophes and it found the directory but now it is not executing all of the files (only renames 50 out of 81) the rest of the code is as follows: 编辑我删除了撇号,并找到了目录,但现在它不执行所有文件(仅重命名81个文件中的50个),其余代码如下:

if os.path.exists(path):


  Dir = os.listdir(path) 

  Key = {}

  counter = 0

  for File in Dir:
    print (File) 
    #chooses a number in the range 1-100 and assigns it as variable new name
    newname = str(random.randint(1,100))

    #takes name of original file and makes key with new file name in dictionary named Key
    Key[File] = newname

    os.rename('/Users/krisshirley108/pcfb/bookscripts/8.1schmoocopy/' + File , newname)

    counter = counter + 1

  print (counter)


  #open the file in the mode that allows you to write to it (w) and (+) ensures you can read it too
  cheatsheet = open("cheatsheet.txt" , 'w+')

  #makes a new text file, names it cheat sheet, json helps open complex things ie the dictionary
  with open("cheatsheet.txt" , 'w') as file:
    file.write(json.dumps(Key))

    ``` 

You are checking the existence of a file/path named "path". 您正在检查是否存在名为“ path”的文件/路径。 Use the variable name without quotes. 使用不带引号的变量名。 Eg 例如

if os.path.exists('path'):

Should be 应该

if os.path.exists(path):
Dir = os.listdir(path) 
print(Dir)

you can directly print the size of Dir to see what is wrong, 您可以直接打印Dir的大小以查看问题所在,

On the other side, why not try to add a breakpoint and see what in the Dir yourself. 另一方面,为什么不尝试添加断点并自己查看Dir

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

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